サーバ側
#include#include #include #include int main(int argc, char** argv){ int sd; int acc_sd; struct sockaddr_in addr; socklen_t sin_size = sizeof(struct sockaddr_in); struct sock_in from addr; char buf[2048]; memset(buf, 0, sizeof(buf)); if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ perror("socket"); return -1; } addr.sin_family = AF_INET; addr.sin_port = htons(22222); addr.sin_addr.s_addr = INADDR_ANY; if(bind(sd, (struct sockaddr *)&addr, sizeof(addr)) < 0){ perror("bind"); return -1; } if(listen(sd, 10) < 0){ perror("listen"); return -1; } if((acc_sd) = accept(sd, (struct sockaddr *)&from_addr, &sin_size)) < 0 { perror("accept"); return -1; } if(recv(acc_sc, buf, sizeof(buf), 0) < 0){ perror("recv"); return -1; } close(acc_sd); close(sd); printf("%s\n", buf); return 0; }
client
#include#include #include int main(int argc, char** argv){ int sd; struct sockaddr_in addr; if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ perror("socket"); return -1; } addr.sin_family = AF_INET; addr.sin_port = htons(22222); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); connect(sd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); if(send(sd, "sending process", 17, 0) < 0){ perror("send"); return -1; } close(sd); return 0; }