In daily life, the majority of people use the machine is usually the Windows system, but for developers, development, compilation and so on is often built on the Linux machine. In fact. On the server side, Linux, Unix, and WindowsServer occupy a large share of the market; in supercomputers, Linux is the first operating system to replace UNIX.
Communication is a major task of computer and operating system, through FTP, ping, ssh and so on. People can be very convenient to connect with the server. A large network of two programs through a two-way communication connection to achieve the exchange of data, one end of this connection is called a socket.
The Windows system uses a Windows socket, while Linux uses a POSIX socket. Today combined with some classic online routines, a simple socket code was written to enable the communication between Ubuntu (virtual machine) and Windows.
Implementation code for the client under the Windows platform:
#include <stdio.h>#include <Windows.h>#pragma comment (lib, "Ws2_32.lib")#define Port#define IP_Address "172.30.70.95"intMainintargcChar* argv[])//ARGC is the total number of parameters in the command line{Wsadata s;//used to store the Windows Sockets initialization information returned by calling the AfxSocketInit global functionSOCKET Clientsocket;structSockaddr_in clientaddr;//A sockaddr_in-type structural body Object intRET =0;CharSendbuffer[max_path];//Windows MAX_PATH default is 260 //Initialize Windows Sockets The //WSAStartup function initializes the Winsock service 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 //Suppose such a socket interface is connected to a specified port with connect () //The datagram can be sent and received with Send () and recv () and the port //When the session ends, call Closesocket ()Clientsocket = socket (Af_inet,//Support ARPA Internet address format onlySock_stream,//Description of type of new set of interfacesIPPROTO_TCP);//protocol used by the socket interface if(Clientsocket = = Invalid_socket) {printf(the 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 addressClientaddr.sin_port = htons (port);//Convert unsigned short shaping of the host to network byte order memset(Clientaddr.sin_zero,0x00,8);the//function usually initializes the newly requested memory //Connect socketret = Connect (Clientsocket, (structsockaddr*) &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);//Send data to serverret = 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 socketClosesocket (Clientsocket);if(sendbuffer[0] ==' Q ')//Set the input to exit when the first character is Q{printf("quit!\n"); Break; }} wsacleanup (); GetChar ();return 0;}
My Linux (Ubuntu) side Network information:
Server code under the Linux (Ubuntu) platform:
#include <unistd.h>#include <stdio.h>#include <i386-linux-gnu/sys/socket.h>#include <netinet/in.h>#include <i386-linux-gnu/sys/types.h>#include <stdlib.h>#include <string.h>#define SERVER_PORT#define Length_of_listen_queue#define Buffer_sizeintMain ()//(int argc, char* argv[]){structSockaddr_in server_addr;intServer_socket;intopt =1; Bzero (&SERVER_ADDR,sizeof(SERVER_ADDR));//byte string the first n bytes is 0, including 'server_addr.sin_family = af_inet; SERVER_ADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any);//Turn to the small end, Inaddr_any is the address assigned to address 0.0.0.0Server_addr.sin_port = htons (Server_port);//Create a socketServer_socket = socket (pf_inet, Sock_stream,0);if(Server_socket <0) {printf("Create Socket failed!\n");Exit(1); }//bind a socketSetSockOpt (Server_socket, Sol_socket, SO_REUSEADDR, &opt,sizeof(opt));if(Bind (Server_socket, (structsockaddr*) &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) {structSockaddr_in client_addr;intClient_socket; socklen_t length;CharBuffer[buffer_size];//Connect client socketLength =sizeof(CLIENT_ADDR); Client_socket = Accept (Server_socket, (structsockaddr*) &client_addr, &length);if(Client_socket <0) {printf("Server Accept failed!\n"); Break; }//Receive 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;}
When the client is opened. Server side to ensure that it is in the listening state, otherwise the connection fails:
Open the server-side program such as the following:
Transfer data successfully:
This little experiment only took very little time to debug, and only sent data in one direction. Single function. It's just a little exercise. In the debugging of socket programming, the main concern is the error value returned when the program goes wrong, which can often find out the vulnerability of code very quickly.
References Link: http://blog.csdn.net/feixiaoxing/article/details/8567162
Small exercise: Using sockets for communication between Linux and Windows