Socket is very familiar with everyone, I am no longer here. Cut the topic directly. The program is divided into two parts, the Server side and the Client side (no one know). Where the Server terminal uses a Windows platform, the Linux platform is of course a client end. Here is the Server-end program: / ******************************************* / / * IT WORKED AS Server Under Windows Platform * // * Written by mlsx 1998-2-7 * // ****************************************************** ** /
Include
main (int argc, char * argv []) {char buf [128]; SOCKET s, ns; struct sockaddr_in client; struct sockaddr_in server; int namelen, pklen; int status; WSADATA wsd; if ((status = WSAStartup (MAKEWORD (2, 2), & WSD))! = 0) {PERROR ("WsaStartup () failed:"); exit (1);} if ((s = socket (AF_INET, SOCK_STREAM, 0) <0) {PERROR ("Socket Failed:"); exit (1);} zeromeMory (& Server, SIZEOF (Server)); server.sin_family = AF_INET; Server.SIN_PORT = HTONS (Userport); Server.sin_Addr.s_addr = HTONS (INADDR_Any); IF (Bind (Struct SockAddr *) & Server, SIZEOF (Server)) <0) {PERROR ("Bind () Failed:"); EXIT (2);} IF (Listen (S, 4)! = 0 ) {Perror ("Listen () failed:"); exit (3);} while (1) {namelen = sizeof (client); if (((ns = accept (s, struct socddr *) & client, & namelen) == - 1) {PERROR ("Accept () failed:"); exit (4);} Printf ("Accept successful"); for (;;) {IF ((pklen = RECV (NS, BUF, 128, 0)) <0) {Perror ("RECV () failed:"); exit (5);} else if (pklen == 0) {Printf ("RECV (): returnified, connection is shutd down"); Break;} else printf ("RE CV (): returnus, packet length =% d context IS% S / N ", PKLEN, BUF); SLEEP (1); // Printf (" The BUF IS% S / N ", BUF); IF (Send (NS, BUF, 128, 0) <0) {PERROR ("Send () failed:"); Break;} else printf ("Send () returnis success packet length is% d / n", pklen);}} CloseSocket (NS); CloseSocket (s); Printf ("Server Ended SuccessFully / N);} This is the client server:
/ ******************************************** / / * IT WORKED AS Client Under Linux Platform * // * Written BY MLSX 1998-2-7 * // ******************************************************************************************************************************************************************************** / # include < Windows.h> // # include