socket编程两台电脑(两个socket)
我在做一个C/S socket编程,在一台电脑上客户端和服务器之间可以正常发送数据,两台就连接失败了。
两台电脑必须都是铁通的,必须都没有在内网(没有使用路由器)。才能连通
如何用SOCKET实现两台计算机之间的通讯
两部分代码,一个server,一个client。
server 端代码:
#include?"stdio.h"??
#include?"Winsock2.h"??
#include?"iostream"??
#include?"string"??
?
//#pragma?comment(lib,?"ws2_32.lib")??
?
//define?host?IP?and?usable?port.??
#define?HOST_IP?127.0.0.1??
#define?HOST_PORT?8080??
#define?OK_STR?"OK"??
?
void?main(){??
????//the?version?bit?of?Winsock??
????int?version_a?=?1;//low?bit??
????int?version_b?=?1;//high?bit??
?
????//makeword??
????WORD?versionRequest?=?MAKEWORD(version_a,version_b);??
????WSAData?wsaData;??
????int?err;??
?
????err?=?WSAStartup(versionRequest,?wsaData);???
?
????if(err?!=?0?){??
????????printf("ERROR!");??
????????return;??
????}??
????if?(LOBYTE(wsaData.wVersion)??!=?1?||?HIBYTE(wsaData.wVersion)?!=?1)??
????{??
????????printf("WRONG?WINSOCK?VERSION!");??
????????WSACleanup();??
????????return;??
????}??
?
????SOCKET?socServer?=?socket(AF_INET,?SOCK_DGRAM,0);??
????SOCKADDR_IN?addr_Srv;??
????//It's?nest?three?structs?as?S_un_b,?S_un_w?and?S-un.(union)??
????addr_Srv.sin_addr.S_un.S_addr?=?htonl(INADDR_ANY);??
????//set?protocol?family??
????addr_Srv.sin_family?=?AF_INET;??
????//set?host?port??
????addr_Srv.sin_port?=?htons(HOST_PORT);??
????//bind?socket?to??the?host??
????bind(socServer,(?SOCKADDR*)?addr_Srv,?sizeof(SOCKADDR));??
?????//address??of?a?client??
????SOCKADDR_IN?addr_Clt;??
????char?recvBuf[100];??
????int?fromlen?=?sizeof(SOCKADDR);??
????while(true){??
????recvfrom(socServer,?recvBuf,?100,?0,??(SOCKADDR*)?addr_Clt,?fromlen);?????std::coutrecvBufstd::endl;??
????sendto(socServer,?OK_STR,?strlen(OK_STR)+1,?0,?(SOCKADDR*)addr_Clt,sizeof(SOCKADDR));??
????}??
????closesocket(socServer);??
????WSACleanup();?
}
client端代码:
#include?"Winsock2.h"??
#include?"iostream"??
#include?"stdio.h"??
#pragma?comment(lib,?"ws2_32.lib")??
?
?
#define?HOST_IP?"127.0.0.1"??
#define?HOST_PORT?8080??
?
void?main(){??
????//the?version?bit?of?Winsock??
????int?version_a?=?1;//low?bit??
????int?version_b?=?1;//high?bit??
???//makeword??
????WORD?versionRequest?=?MAKEWORD(version_a,version_b);??
????WSAData?wsaData;??
????int?error;??
????error?=?WSAStartup(versionRequest,?wsaData);???
????if(error?!=?0???){??
????????printf("ERROR!");??
????????return;??
????}??
????if?(LOBYTE(wsaData.wVersion)??!=?1?||?HIBYTE(wsaData.wVersion)?!=?1)??
????{??
????????printf("WRONG?WINSOCK?VERSION!");??
????????WSACleanup();??
????????return;??
????}??
????//request?info?obtained?by?user's?input??
????char?requestStr[100];??
????//build?a?sockeet???
????SOCKET?socClient?=?socket(AF_INET,?SOCK_DGRAM,?0);??
????SOCKADDR_IN?addrSrv;?????????
????addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");?????????
????addrSrv.sin_family=AF_INET;?????//set?the?protocol?family??
????addrSrv.sin_port=htons(HOST_PORT);??????//set?the?port?number??
????//?array?to?store?the?data?that?server?feedback.??
????char?cRecvBuf[100];??
???while(true){??
?????????std::cinrequestStr;??
????????//sendto?function?is?used?to?send?programe?data?to?the?server??
????????sendto(socClient,?requestStr,?strlen(requestStr)+1,?0,?(SOCKADDR*)?addrSrv,?sizeof(SOCKADDR));??
?????????recv(socClient,?cRecvBuf,?strlen(cRecvBuf)+1,?0);??
?????????std::cout?cRecvBuf??std::endl;??
????}??
??????
????//close?the?socket?and?cleanup?the?wsadata??
????closesocket(socClient);??
????WSACleanup();??
//??return?0;??
}
java中socket实现两天电脑连接IP怎么写啊? 求高手指点
Socket
分为服务器端和客户端
连接时,服务器端用某个端口打开socket,然后监听
客户端用ip和端口连接,被接收则连接成功。
因此,服务器端(你可以随意指定一个电脑为服务器端
)打开端口连接:
ServerSocket
serversocket
=
new
ServerSocket(port);
Socket
socket
=
serverSocket.accept();//监听客户端的连接
然后客户端:
Socket
socket=new
Socket(ip,port);//这里的ip也可以用域名
希望对你有所帮助~
基于Socket编程实现两台Linux计算机之间传输文件的程序netcp,实现命令行方式同步推送。急用,谢谢
1、采用socket的tcp协议开发,采用server/client架构;
2、首先在两台计算机上开发一个deamon程序,后台运行server,解释远程客户端的命令;
3、开发客户端程序netcp,解释输入参数,并从远程服务器获取文件,写入本机的目录中。
xdm,,pythonsocket一台电脑可以通信,两台电脑
xdm,pythonsocket一台电脑可以通信两台电脑。使用python的socket编程实现两台电脑之间的通信可以使用两种方式完成。python开发简单socket程序在两台电脑之间传输消息,分为客户端和服务端,分别在两台电脑上运行后即可进行简单的消息传输,也可在一台电脑上测试,设置两个不同的端口。