//TCP Server //Includes #include #include #include #include #include #include #include #include #include #include //Function Prototypes void myabort(char *); //Some Global Variables int serverport = 3000; char * eptr = NULL; int listen_socket, client_socket; struct sockaddr_in Server_Address, Client_Address; int result; socklen_t csize; //main() int main(int argc, char **argv){ char buf[100]; char tmp[100]; char * ptr; int n, sent, length; //Step 0: Process Command Line if (argc > 2){ myabort("Usage: server "); } if (argc == 2){ serverport = (int) strtol(argv[1], &eptr, 10); if (*eptr != '\0') myabort("Invalid Port Number!"); } //Step 1: Create a socket listen_socket = socket(PF_INET, SOCK_STREAM, 0); if (listen_socket == -1) myabort("socket()"); //Step 2: Setup Address structure bzero(&Server_Address, sizeof(Server_Address)); Server_Address.sin_family = AF_INET; Server_Address.sin_port = htons(serverport); Server_Address.sin_addr.s_addr = INADDR_ANY; //Step 3: Bind the socket to the port result = bind(listen_socket, (struct sockaddr *) &Server_Address, sizeof(Server_Address)); if (result == -1) myabort("bind()"); //Step 4:Listen to the socket result = listen(listen_socket, 1); if (result == -1) myabort("listen()"); //Step 5: Setup an infinite loop to make connections while(1){ //Step 5a: Accept a Connection csize = sizeof(Client_Address); client_socket = accept( listen_socket, (struct sockaddr *) &Client_Address, &csize ); if (client_socket == -1) myabort("accept()"); cout << "Client Accepted!" << endl; //Step 5b: Communicate with client //loop until client closes while (1){ //read string from client bzero(&buf, sizeof(buf)); do{ bzero(&tmp, sizeof(tmp)); n = read(client_socket,(char *) &tmp, 100); //cout << "server: " << tmp; tmp[n] = '\0'; if (n == -1) myabort("read()"); if (n == 0) break; strncat(buf, tmp, n-1); buf[n-1] = ' '; } while (tmp[n-1] != '\n'); buf[ strlen(buf) ] = '\n'; if (n == 0) break; //write string back to client sent = 0; ptr = buf; length = strlen(buf); while (sent < length ){ n = write(client_socket, ptr, strlen(ptr) ); if ( n == -1) myabort("write()"); sent += n; ptr += n; } }//end inner while cout << "Client closed! Listening for other clients." << endl; close(client_socket); } close(listen_socket); exit(0); } void myabort(char * msg){ cout << "Error!: " << msg << endl; exit(1); }