echoserver.c
//coded by m4n1G //6-1-2014 //tcp echo server in unix env #include "stdlib.h" #include "sys/types.h" #include "sys/socket.h" #include "netinet/in.h" #include "arpa/inet.h" int main(int argc, char** argv) { int server_fd, client_fd; char c; struct sockaddr_in serv_addr; //create a socket server_fd = socket(AF_INET,SOCK_STREAM,0); //getting ready to bind serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(atoi(argv[1])); serv_addr.sin_addr.s_addr = INADDR_ANY; //bind bind(server_fd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)); //listen to one queue listen(server_fd,1); //accept one client client_fd = accept(server_fd, NULL, NULL); //read and write until ^D from client while(read(client_fd, &c, 1)) { //read from client putchar((int)c); write(client_fd, &c, 1); // write to client } //close the connection close(client_fd); close(server_fd); return 0; }echoclient.c
//coded my m4n1g //on 6-1-2014 //tcp echo client #include "stdio.h" #include "stdlib.h" #include "sys/types.h" #include "sys/socket.h" #include "netinet/in.h" #include "arpa/inet.h" int main(int argc, char** argv) { int client_fd; struct sockaddr_in serv_addr; char c; //create socket client_fd = socket(AF_INET,SOCK_STREAM,0); //ready to bind serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(atoi(argv[2])); serv_addr.sin_addr.s_addr = inet_addr(argv[1]); //connect to server connect(client_fd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)); //write to echo server while((c=(char) getchar())!=EOF) { write(client_fd,&c,1); read(client_fd,&c,1); putchar((int)c); } close(client_fd); return 0; }
Comments
Post a Comment