#include #include //for connection and various #include #include //for memset #include //perror #include #include //inet_pton and ntop int main() { char hostname[] = "127.0.0.1"; //this means local host sockaddr_in addr; int s = socket(AF_INET, SOCK_STREAM, 0); bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(23000); //host to network byte ordering (short) printf("binding\n"); int e = bind(s, (sockaddr *)&addr, sizeof(addr)); if(e < 0 ) { perror("binding error: "); return 0; } printf("listening\n"); e = listen(s,1); if(e < 0 ) perror("listen error: "); while(1) { printf("accepting?"); sockaddr_in client; socklen_t len = sizeof(addr); // int c = accept(s, NULL, NULL); int c = accept(s, (sockaddr *)&client, &len); char c2[256]; inet_ntop(AF_INET,& (client.sin_addr),c2,256); printf("Client is: %s : %d \n",c2, ntohs(client.sin_port) ); char msg[] = "hi client"; printf("Sending: %s\n",msg); int s =send(c,msg,strlen(msg),0); if(s < 0) perror("Send: "); printf("Sent %d, now sleeping\n",s); sleep(10); //take a brief nap shutdown(c,2); close(c); } }