#include #include #include #include #include #include #define SERVER_IP "192.168.7.8" #define SRC_PORT 50550 #define DST_PORT 2500 #define xerror(str) do { perror(str); exit(1); } while (0) int main(void) { struct sockaddr_in sa; int s; int t = 0; char buf[32]; int ret, tmp; memset(&sa, 0, sizeof(sa)); for (t = 1; t <= 1; t++) { switch (fork()) { case -1: xerror("fork"); break; case 0: printf("client %d binding port...\n", getpid()); s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (!s) xerror("socket"); sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_ANY); sa.sin_port = htons(SRC_PORT); tmp = 1; if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&tmp, sizeof(tmp)) != 0) xerror("setsockopt"); if (bind(s, (struct sockaddr*)&sa, sizeof(sa)) != 0) xerror("bind"); printf("client %d connecting...\n", getpid()); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr(SERVER_IP); sa.sin_port = htons(DST_PORT); if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) != 0) xerror("connect"); printf("client %d waiting in read()...\n", getpid()); ret = read(s, buf, 32); printf("client %d read() returned %d\n", getpid(), ret); close(s); return 0; } wait(NULL); } return 0; }