1. 程式人生 > >隨想錄(windows和linux進行socket通訊)

隨想錄(windows和linux進行socket通訊)

【 宣告:版權所有,歡迎轉載,請勿用於商業用途。  聯絡信箱:feixiaoxing @163.com】 

    在公司裡面,我們平時使用的機器一般都是windows系統,但是開發、編譯的機器往往是linux伺服器。通過ping、ftp、samba、telnet、ssh,人們可以很方便與伺服器連線。windows系統使用的是windows socket,而linux使用的posix socket,那麼這兩個socket之間是怎麼通訊的呢?網上關於windows與windows通訊的程式碼很多,linux與linux通訊的程式碼也很多,但是windows和linux通訊的程式碼很少。這裡,我就想寫一個簡單的socket程式碼,實現linux和windows的通訊。

    其實,廣義一點看,網頁訪問、郵箱、遊戲、聊天工具都是利用socket實現的,當然後面實現的邏輯要比我們寫的程式碼複雜的多。但是,我們完全可以利用簡單的程式碼來說明socket通訊問題,能做到這一點就足夠了。這也是我一直推崇的方法,用最簡單的程式碼表達最實際的功能。

    windows側的client程式碼,

#include <stdio.h>
#include <Windows.h>
 
#pragma comment(lib,"ws2_32.lib")
#define  PORT 4000
#define  IP_ADDRESS "192.168.1.102"
 
int main(int argc, char* argv[])
{
    WSADATA  Ws;
    SOCKET ClientSocket;
    struct sockaddr_in ClientAddr;
    int Ret = 0;
    char SendBuffer[MAX_PATH];

    /* Init Windows Socket */
    if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
    {
        printf("Init Windows Socket Failed::%d\n", GetLastError());
        return -1;
    }
 
    /* Create Socket */
    ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if ( ClientSocket == INVALID_SOCKET )
    {
        printf("Create Socket Failed::%d\n", GetLastError());
        return -1;
    }

    ClientAddr.sin_family = AF_INET;
    ClientAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
    ClientAddr.sin_port = htons(PORT);
    memset(ClientAddr.sin_zero, 0x00, 8);

	/* connect socket */
    Ret = connect(ClientSocket,(struct sockaddr*)&ClientAddr, sizeof(ClientAddr));
    if ( Ret == SOCKET_ERROR )
    {
        printf("Connect Error::%d\n", GetLastError());
        return -1;
    }
    else
    {
        printf("Connect succedded!\n");
    }

    while (1)
    {
        scanf("%s", SendBuffer);

		/* send data to server */
        Ret = send(ClientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
        if ( Ret == SOCKET_ERROR )
        {
            printf("Send Info Error::%d\n", GetLastError());
            break;
        }
		
		if('q' == SendBuffer[0])
		{
			break;
		}
    }
    
	/* close socket */
    closesocket(ClientSocket);
    WSACleanup();
 
    return 0;
 }
 

    linux側的server程式碼,
#include <netinet/in.h>    
#include <sys/types.h>    
#include <sys/socket.h>    
#include <stdio.h>        
#include <stdlib.h>       
#include <string.h>       

#define HELLO_WORLD_SERVER_PORT    4000
#define LENGTH_OF_LISTEN_QUEUE 20
#define BUFFER_SIZE 1024

int main(int argc, char **argv)
{
    struct sockaddr_in server_addr;
	int server_socket;
	int opt = 1;
   
    bzero(&server_addr,sizeof(server_addr)); 
	
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htons(INADDR_ANY);
    server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);

	/* create a socket */
    server_socket = socket(PF_INET,SOCK_STREAM,0);
    if( server_socket < 0)
    {
        printf("Create Socket Failed!");
        exit(1);
    }
 
    /* bind socket to a specified address*/
    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!", HELLO_WORLD_SERVER_PORT); 
        exit(1);
    }

    /* listen a socket */
    if(listen(server_socket, LENGTH_OF_LISTEN_QUEUE))
    {
        printf("Server Listen Failed!"); 
        exit(1);
    }
	
	/* run server */
    while (1) 
    {
        struct sockaddr_in client_addr;
		int client_socket;		
        socklen_t length;
		char buffer[BUFFER_SIZE];

		/* accept socket from client */
		length = sizeof(client_addr);
        client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length);
        if( client_socket < 0)
        {
            printf("Server Accept Failed!\n");
            break;
        }
        
		/* receive data from 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 from client!\n");
				break;
			}
			
			printf("%s\n", buffer);
		}	
		
        close(client_socket);
    }
	
    close(server_socket);
    return 0;
}

    windows側的程式碼使用vc6.0編譯就可以,而linux側的程式碼用gcc就ok了,即gcc server.c -o server。首先,在linux側輸入./server,然後開啟widnows側的client程式,然後在windows側的每一個字元輸入都會在linux打印出來。如果想退出,輸入q即可。但是此時server不會退出,它在等待下一個client的通訊,繼續服務於大家,就是這麼簡單。

    這裡的server和client程式碼是根據網友的程式碼修改而來,一併表示感謝。如有侵權,請郵件告知。