Home > Software engineering >  Socket programming the accept () always returns SOCKET_ERROR
Socket programming the accept () always returns SOCKET_ERROR

Time:10-04

Write a simple c language socket programming, I use a while loop blocking the accept () until the client visit, what has happened is that the client can connect successfully, but is the server always didn't return the correct accept, has been stuck in a cycle, unable to take the next step the recv ()

The server-side code:
 # include 
#include

using namespace std;



Void ErrorHandling (char * Line) {
Cout & lt; system("pause");
The exit (1);
}

Int main () {

WSADATA WSADATA;
Sockaddr_in seraddr clnadr;
The SOCKET ServiceSocket, ClientSocket;
Int ClientAdrSize;

Char message [100]="NULL";
//char * server_ip="192.168.1.105";
Char * port="8801";
Int strlen=0;
Const int BUF_SIZE=100;

If (WSAStartup (MAKEWORD (2, 2), & amp; Wsadata)!=0)
WSAStartup ErrorHandling (" Error!" );

ServiceSocket=socket (PF_INET SOCK_STREAM, IPPROTO_TCP);


If (ServiceSocket==INVALID_SOCKET) {
ErrorHandling (" ERROR ");
system("pause");
The exit (1);
}

Cout & lt; <"Socket create success!" Memset (& amp; Seraddr, 0, sizeof (seraddr));
Seraddr. Sin_family=AF_INET;
Seraddr. Sin_addr. S_addr=htonl (INADDR_ANY);
Seraddr. Sin_port=htons (atoi (port));



If (bind (ServiceSocket, (SOCKADDR *) & amp; Seraddr, sizeof (seraddr))==SOCKET_ERROR)
ErrorHandling (" Bind Error!" );
If (listen (ServiceSocket, 5)==SOCKET_ERROR)
Linsten ErrorHandling (" ERROR ");
Cout & lt; <"The bind () sucess" & lt;
While (1) {//the problem is here, the program has been return socket_error
If (ClientSocket=accept (ServiceSocket, (sockaddr *) & amp; Clnadr, & amp; ClientAdrSize)==SOCKET_ERROR)
continue;
The else {
Cout & lt; <"Connected!" The recv (ServiceSocket, message, BUF_SIZE, 0);
break;
}
}
Cout & lt;

system("pause");


Return (0);
}



The client code:
 # include 
#include
#include
using namespace std;

Void ErroHandling (char * ErrorLine) {
Cout & lt; system("pause");
The exit (1);
}


Int main () {
Int const BUF_SIZE=100;
Int strlen_;
Char message [BUF_SIZE];
Char * ipaddr="192.168.1.105";
Char * port="8801";

WSADATA WSADATA;
The SOCKET ServerSocket.
SOCKADDR_IN SerAddr;

If (WSAStartup (MAKEWORD (2, 2), & amp; Wsadata)!=0)
WSAstartup ErroHandling (" Error ");

The fgets (message, BUF_SIZE, stdin);
Cout & lt; ServerSocket=socket (PF_INET SOCK_STREAM, 0).
Memset (& amp; SerAddr, 0, sizeof (SerAddr));
SerAddr. Sin_family=AF_INET;
SerAddr. Sin_addr. S_addr=inet_addr (ipaddr);
SerAddr. Sin_port=htons (atoi (port));
While (1) {
If (connect (ServerSocket (sockaddr *) & amp; SerAddr, sizeof (SerAddr))==SOCKET_ERROR)
continue;
The else {
Cout & lt; <"Connected!" Send (ServerSocket, message, strlen (the message), 0).//send a message here
break;
}

}
system("pause");
Strlen_=recv (ServerSocket, message, BUFSIZ - 1, 0).//receiving the news of the server returns


Closesocket (ServerSocket);
WSACleanup ();
system("pause");
Return (0);
}


Help the thigh ~ help ~

CodePudding user response:

The priority of the assignment operator=is smaller than the==comparison operator, server if the accept, try to add a bracket?
 if ((ClientSocket=accept (ServiceSocket, (sockaddr *) & amp; Clnadr, & amp; ClientAdrSize))==SOCKET_ERROR) 

CodePudding user response:

A print accept return values will know whether to have returned, as well as your subsequent processing is correct

CodePudding user response:

 
Int ClientAdrSize=sizeof (sockaddr_in);

CodePudding user response:

The SOCKET accept (
_In_ SOCKET s,
_Out_ struct sockaddr * addr,
_Inout_ int * addrlen
);

The Parameters

S [in]
A descriptor that identifies A socket that has had been placed in A listening state have the listen function. The connection is later made with the socket that is returned by accept.

Addr [out]
An optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family that was established when the socket from the sockaddr structure was created.

Addrlen [ , in the out]
An optional pointer to An integer that contains the length of structure pointed to by the addr parameter.

CodePudding user response:

#include
  • Related