socket
socket

Reputation: 1203

socket TCP server program problems

The following code is my TCP server program:

    #include
    #include
    #pragma comment (lib,"ws2_32.lib")
    #define PORT 8888
    #define ADDR "127.0.0.1"

int main() { WSADATA wsock; SOCKET listensocket,connectsocket; SOCKADDR_IN seraddr,cliaddr; int cliaddrsize=sizeof(cliaddr); int nret=0; char buf[100]; printf("init socket ...\n"); if(WSAStartup(MAKEWORD(2,2),&wsock)!=0) { printf("WSAStartup() failed %d\n",WSAGetLastError()); return 0; } printf("init successfully\n"); printf("create TCP socket...\n"); if((listensocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET) { printf("socket create failed %d\n",WSAGetLastError()); WSACleanup(); return 0; } printf("socket create successfully\n"); seraddr.sin_family=AF_INET; seraddr.sin_addr.s_addr=inet_addr(ADDR); seraddr.sin_port=htons(PORT); if(bind(listensocket,(SOCKADDR *)&seraddr,sizeof(seraddr))==SOCKET_ERROR) { printf("bind failed %d\n",WSAGetLastError()); closesocket(listensocket); WSACleanup(); return 0; } printf("bind successfully\n"); if(listen(listensocket,5)==SOCKET_ERROR) { printf("listen failed %d\n",WSAGetLastError()); closesocket(listensocket); WSACleanup(); return 0; } printf("wait for a connection on port %d\n",PORT); if(connectsocket=accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrsize)==INVALID_SOCKET) //accept { printf("accept failed %d\n",WSAGetLastError()); closesocket(listensocket); WSACleanup(); return 0; } printf("get connection from %s : %d successfully\n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));//NB 啊 closesocket(listensocket); printf("wait to receive data...\n"); memset(buf,0,sizeof(buf)); while(1) { if(nret=recv(connectsocket,buf,sizeof(buf),0)==SOCKET_ERROR) //recv { printf("recv failed %d\n",WSAGetLastError()); closesocket(connectsocket); WSACleanup(); return 0; } printf(buf); printf("\n"); if(strncmp(buf,"exit",sizeof("exit"))==0) { printf("exit the loop\n"); break; } if(nret=send(connectsocket,buf,sizeof(buf),0)==SOCKET_ERROR) { printf("send failed %d\n",WSAGetLastError()); } } closesocket(connectsocket); WSACleanup(); return 0; }

And I use my network debugging assistent software to run as a TCP client. As follows:

I started my TCP server program, it went as follows:

(...cannot upload images...)

But when I click on my TCP client, the TCP server went wrong : the dos box shows that : recv failed 10038

My question is why it went wrong ? And how to fix it?

Upvotes: 0

Views: 707

Answers (2)

hmjd
hmjd

Reputation: 121971

This:

if(connectsocket=accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrsize)
       ==INVALID_SOCKET)

will result in connectsocket having a value of 0, which does not refer to a valid socket descriptor which the error code 10038 means:

An operation was attempted on something that is not a socket.

because of operator precedence:

// Result of this will be 0 (false) when result of accept()
// is not `INVALID_SOCKET`
accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrsize) == INVALID_SOCKET

// Which is then assigned to connectsocket
connectsocket = 0

// And the failing if branch is not entered
if (connectsocket)

You need parenthesis around the assignment:

if((connectsocket=accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrsize))
    ==INVALID_SOCKET)

which you have already done for the earlier call to socket().

Upvotes: 4

sirgeorge
sirgeorge

Reputation: 6531

You have an error in line :

if(connectsocket=accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrsize)==INVALID_SOCKET)  //accept

It should be:

if((connectsocket=accept(listensocket,(SOCKADDR*)&cliaddr,&cliaddrsize))==INVALID_SOCKET)  //accept

Notice the parenthesis. In your original code you are assigning the result of the comparison to a connectsocket variable (which will be always zero as a result).

Upvotes: 2

Related Questions