Home > Back-end >  O great god answer socket communication problems. In the unit test everything is normal, but unable
O great god answer socket communication problems. In the unit test everything is normal, but unable

Time:10-11

Socket as shown in figure, made of a small program, sending pictures on one machine test can normal use, but the use of the inside of the local area network (LAN) 2 machine when do the test we found there is connection, firewall closed, also ping tong, strives for the great god answers of all stripes, urgent need

CodePudding user response:

Does anyone help me solve the problem, and the younger brother's side project to use, software written only to find that can't use too tragedy

CodePudding user response:

Look on the post code, so there is no way to know

CodePudding user response:

If (WSAStartup (MAKEWORD (2, 2), & amp; Dat)!=0) {}
Ret=0;
//create a Socket
ClientSocket=socket (AF_INET SOCK_STREAM, IPPROTO_TCP);
If (clientSocket==INVALID_SOCKET) {}

ServerAddr. Sin_family=AF_INET;
ServerAddr. Sin_addr. S_addr=inet_addr (str_ip_touch c_str ());
ServerAddr. Sin_port=htons (12121);
Memset (serverAddr. Sin_zero, 0, sizeof (serverAddr. Sin_zero));


Ret=connect (clientSocket, (struct sockaddr *) & amp; ServerAddr, sizeof (serverAddr));
If (ret==SOCKET_ERROR) {}
Ret=0;
Out=ofstream (str_img_path + str_img_name fstream: : out | fstream: : binary);
for (int i=0; i<1024; I++)
{
Buf [I]=0;
}
While (1)
{
Ret=recv (clientSocket, buf, 1024, 0);
If (ret> 0)
{
Out. Write (buf, sizeof (char) * ret);

}
The else
{
out.close();
break;
}
}
out.close();
Closesocket (clientSocket);

CodePudding user response:

This is the code at the receiving end

CodePudding user response:

Ret=0;
//initialize the Socket
If (WSAStartup (MAKEWORD (2, 2), & amp; Dat)!=0) {}
//Socket return value
//create a Socket
ServerSocket=socket (AF_INET SOCK_STREAM, IPPROTO_TCP);
If (serverSocket==INVALID_SOCKET) {}
LocalAddr. Sin_family=AF_INET;
LocalAddr. Sin_addr. S_addr=inet_addr (str_ip_wall c_str ());
LocalAddr. Sin_port=htons (12121);
Memset (localAddr. Sin_zero, 0, sizeof (localAddr. Sin_zero));
//server
Ret=bind (serverSocket, (struct sockaddr *) & amp; LocalAddr, sizeof (localAddr));
If (ret!=0) {}
//the server monitor
Ret=listen (serverSocket, 10);
If (ret!=0) {}
While (1)
{
//receive information
Int addLen=sizeof (clientAddr);
ClientSocket=accept (serverSocket, (struct sockaddr *) & amp; ClientAddr, & amp; AddLen);
String ipath;
Ipath="D: \ \ ucc_touch \ \ ucc \ " + str_imgname;
Ifstream in (ipath fstream: : | in fstream: : binary);
Char buf [1024]={0};
while(! In the eof ())
{
In the read (buf, sizeof (buf));
Ret=send (clientSocket, buf, 1024, 0);
If (ret==SOCKET_ERROR)
{
break;
}
}
in.close();
Closesocket (clientSocket);
break;
}
Closesocket (serverSocket);
WSACleanup ();
This is the sender
  • Related