Examples of socket traffic under Linux (client) and Windows (server side):
(1) The first is windows to do the client, Linux to do the service-side program
Windows Client Side
- #include <stdio.h>
- #include <Windows.h>
- #pragma comment (lib, "Ws2_32.lib")
- #define PORT 5000
- #define IP_Address "192.168.1.30"//server address
- int main ()//ARGC is the total number of arguments on the command line
- {
- Wsadata s; Used to store the Windows Sockets initialization information returned by calling the AfxSocketInit global function
- SOCKET Clientsocket;
- struct sockaddr_in clientaddr; A sockaddr_in type of structure object
- int ret = 0;
- Char Sendbuffer[max_path]; Windows MAX_PATH Default is 260
- Initialize the Windows Socket
- Initialization of the Winsock service by the WSAStartup function
- if (WSAStartup (Makeword (2, 2), &s)! = 0)//By connecting two given unsigned parameters, the first parameter is a low byte
- {
- printf ("Init Windows Socket failed! Error:%d\n ", GetLastError ());
- GetChar ();
- return-1;
- }
- while (1)
- {
- Create a set of interfaces
- If such a socket is connected to a specified port with connect ()
- Send () and recv () are available for sending and receiving datagrams with this port
- When the session ends, call Closesocket ()
- Clientsocket = socket (af_inet,//Only ARPA Internet address format is supported
- Sock_stream,//Type description of the new set of interfaces
- IPPROTO_TCP); protocol used by the socket interface
- if (Clientsocket = = Invalid_socket)
- {
- printf ("Create Socket failed! Error:%d\n ", GetLastError ());
- GetChar ();
- return-1;
- }
- clientaddr.sin_family = af_inet;
- CLIENTADDR.SIN_ADDR.S_ADDR = inet_addr (ip_address); Define IP Address
- Clientaddr.sin_port = htons (port); Convert the unsigned short number of hosts to network byte order
- memset (Clientaddr.sin_zero, 0x00, 8); Function typically initializes the newly requested memory
- Connecting sockets
- ret = connect (Clientsocket,
- (struct sockaddr*) &clientaddr,
- sizeof (CLIENTADDR));
- if (ret = = socket_error)
- {
- printf ("Socket Connect failed! Error:%d\n ", GetLastError ());
- GetChar ();
- return-1;
- }
- Else
- {
- printf ("Socket Connect succeed!");
- }
- printf ("Input Data:");
- while (1)
- {
- scanf ("%s", &sendbuffer);
- Sending data to the server
- ret = Send (Clientsocket,
- Sendbuffer,
- (int) strlen (sendbuffer),//return send buffer data length
- 0);
- if (ret = = socket_error)
- {
- printf ("Send Information failed! Error:%d\n ", GetLastError ());
- GetChar ();
- Break
- }
- Break
- }
- Close socket
- Closesocket (Clientsocket);
- if (sendbuffer[0] = = ' Q ')//Set input when the first character is Q to exit
- {
- printf ("quit!\n");
- Break
- }
- }
- WSACleanup ();
- GetChar ();
- System ("pause");
- return 0;
- }
Linux Server Side
- #include <stdio.h>
- #include <sys/socket.h>
- #include <sys/types.h>/* See NOTES * *
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <string.h>
- #include <unistd.h>
- #include <cstdlib>
- #define SERVER_PORT 5000
- #define LENGTH_OF_LISTEN_QUEUE 20
- #define BUFFER_SIZE 10
- int main ()//(int argc, char* argv[])
- {
- struct sockaddr_in server_addr;
- int server_socket;
- int opt = 1;
- Bzero (&server_addr, sizeof (SERVER_ADDR)); The first n bytes of a byte string are 0, including '
- server_addr.sin_family = af_inet;
- SERVER_ADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any); To the small end, Inaddr_any is the address assigned to address 0.0.0.0
- Server_addr.sin_port = htons (Server_port);
- Create a socket
- Server_socket = socket (pf_inet, sock_stream, 0);
- if (Server_socket < 0)
- {
- printf ("Create Socket failed!\n");
- Exit (1);
- }
- Bind a socket
- SetSockOpt (Server_socket, Sol_socket, so_reuseaddr, &opt, sizeof (opt));
- if (Bind (Server_socket, (struct sockaddr*) &server_addr, sizeof (SERVER_ADDR)))
- {
- printf ("Server Bind Port:%d failed!\n", server_port);
- Exit (1);
- }
- Monitor socket
- if (Listen (Server_socket, Length_of_listen_queue))
- {
- printf ("Server Listen failed!\n");
- Exit (1);
- }
- while (1)
- {
- struct sockaddr_in client_addr;
- int client_socket;
- socklen_t length;
- Char Buffer[buffer_size];
- Connecting the client socket
- length = sizeof (CLIENT_ADDR);
- Client_socket = Accept (Server_socket, (struct sockaddr*) &client_addr, &length);
- if (Client_socket < 0)
- {
- printf ("Server Accept failed!\n");
- Break
- }
- Receiving data from the client
- while (1)
- {
- Bzero (Buffer, buffer_size);
- Length = recv (Client_socket, Buffer, buffer_size, 0);
- if (length < 0)
- {
- printf ("Server recieve Data failed!\n");
- Break
- }
- if (' q ' = = Buffer[0])
- {
- printf ("quit!\n");
- Break
- }
- printf ("%s\n", Buffer);
- Break
- }
- Close (Client_socket);
- }
- Close (Server_socket);
- return 0;
- }
(2) Windows as server, Linux as client instance
(8) Socket communication instance under Linux (client) and Windows (server side)