Hi;
I have a client/server but i keep getting an error when i run the program. When the socket is blocking it works but non blocking it doesnt.
Here is the server code
Client code isCode:#include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include <fcntl.h> #define BUFFSIZE 256 void Die(char *mess) { perror(mess); exit(1); } int main(int argc, char *argv[]) { int serversock, clientsock; struct sockaddr_in echoserver, echoclient; char buffer[BUFFSIZE]; int val; if (argc != 3) { fprintf(stderr, "USAGE: echoserver <ipaddress> <port>\n"); exit(1); } /* Create the TCP socket */ if ((serversock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { Die("Failed to create socket"); } /*Unblock the socket*/ fcntl(serversock, F_SETFL, O_NONBLOCK); /* Construct the server sockaddr_in structure */ memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */ echoserver.sin_family = AF_INET; /* Internet/IP */ echoserver.sin_addr.s_addr = inet_addr(argv[1]); /* Incoming addr */ echoserver.sin_port = htons(atoi(argv[2])); /* server port */ /* Bind the server socket */ if (bind(serversock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0) { Die("Failed to bind the server socket"); } /* Listen on the server socket */ if (listen(serversock, 5) < 0) { Die("Failed to listen on server socket"); } /* Run until cancelled */ while (1) { unsigned int clientlen = sizeof(echoclient); /* Wait for client connection */ if ((clientsock = accept(serversock, (struct sockaddr *) &echoclient, &clientlen)) < 0) { Die("Failed to accept client connection"); } fprintf(stdout, "Client connected: %s\n", inet_ntoa(echoclient.sin_addr)); } }
Client error on command prompt is Failed to connect with server: Operation now in progressCode:#include <stdio.h> #include <sys/socket.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include <signal.h> #include <sys/time.h> #include <fcntl.h> #include <sys/types.h> #define BUFFSIZE 265 void Die(char *mess) { perror(mess); exit(1); } int main(int argc, char *argv[]) { int sock; struct sockaddr_in echoserver; unsigned int echolen; if (argc != 3) { fprintf(stderr, "USAGE: TCPecho <ip address> <port>\n"); exit(1); } /* Create the TCP socket */ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { Die("Failed to create socket"); } fcntl(sock, F_SETFL, O_NONBLOCK); /*Construct the server sockaddr_in structure */ memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */ echoserver.sin_family = AF_INET; /* Internet/IP */ echoserver.sin_addr.s_addr = inet_addr(argv[1]); /* IP address */ echoserver.sin_port = htons(atoi(argv[2])); /* server port */ /* Establish connection */ if (connect(sock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0) { Die("Failed to connect with server"); } }
Server error is :
Failed to accept client connection: Resource temporarily unavailable!!
Any ideas?
Thanks



LinkBack URL
About LinkBacks



