Write the Windows and Linux Communication Programs with Socket

zhaozj2021-02-16  48

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 #include #pragma Comment (lib, "ws2_32.lib") # define userport 10001 # define host_ip_addr "127.0.0.1"

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 #include #pragma Comment (lib, "ws2_32.lib") # define userport 5003main (int Argc, char * argv []) {char Buf [ 128]; SOCKET SCLIENT; STRUCT SOCKADDR_IN Server; Int Namelen, Pklen; Int Status, Ret; Char Szmessage [128]; WSADATA WSD; IF (Argc <2) {Printf ("USAGE: / N"); Printf ("%) S ipaddr ", __ file__); Printf (" iPaddr is the server's ip address, like xxx.xxx.xxx.xxx/N "); exit (1);} if ((status = wsastartup (MakeWord (2, 2), & WSD))! = 0) {PERROR ("WSAStartup () failed:"); exit (1);} if ((SCLIENT = Socket, Sock_Stream, 0)) <0) {Perror ("Socket Failed:" ); exit (1);} ZeroMemory (& server, sizeof (server)); ZeroMemory (szmessage, sizeof (szmessage)); server.sin_family = AF_INET; server.sin_port = htons (USERPORT); server.sin_addr.s_addr = inet_addr (Argv [1]); IF (Connect, SCLIT SockAddr *) & Server, Si ZEOF ("Connect () FAILED:"); Printf ("% D", wsagetlasterror ()); exit (3);} namelen = sizeof (splor); / * for = 0; RET <10; RET ) {IF (SClient, Szmessage, Sizeof (Szmessage), 0) <0) {Perror ("Send () failed:"); Break;} printf ("Send () Return Success / N Context IS% S / N ", Szmessage); IF ((pklen = Recv (SClient, BUF, SIZEOF (BUF), 0)) <0) {PERROR (" RECV () Failed: "); EXIT 5);} else if (pklen == 0) {Printf ("RECV (): returnified, connection is shutd down"; break;} else {printf ("the pktlen is% d / n"

转载请注明原文地址:https://www.9cbs.com/read-23154.html

New Post(0)