Linux下實現客戶端和服務器端的通訊

首先,能夠將代碼複製下來放到U盤裏,而後掛載到Linux上c++

掛載步驟服務器

找到設備->USB->你U盤的名字socket

 

掛載成功函數

訪問U盤把代碼拷貝到home文件夾下,就能夠直接進行編譯。this

client.c spa

    #include <stdio.h> 
    #include <unistd.h>
    #include <strings.h>
    #include<string.h>
    #include <sys/types.h> 
    #include <sys/socket.h> 
    #include <stdlib.h>
    #include <netinet/in.h> 
    #include <netdb.h>        

    #define PORT 1234   
    #define MAXDATASIZE 100  

    char receiveM[100];
        char sendM[100]; 

    int main(int argc, char *argv[]) 
 { 
    int fd, numbytes;    
    struct hostent *he;       
    struct sockaddr_in server;  

    //檢查用戶輸入,若是用戶輸入不正確,提示用戶正確的輸入方法
    if (argc !=2) {         printf("Usage: %s <IP Address>\n",argv[0]); 
    exit(1); 
    } 

    // 經過函數 gethostbyname()得到字符串形式的ip地址,並賦給he
    if ((he=gethostbyname(argv[1]))==NULL){  
    printf("gethostbyname() error\n"); 
    exit(1); 
    } 


    // 產生套接字fd
    if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){ 
    printf("socket() error\n"); 
    exit(1); 
    } 

    bzero(&server,sizeof(server));

    server.sin_family = AF_INET; 
    server.sin_port = htons(PORT); 
    server.sin_addr = *((struct in_addr *)he->h_addr);  
    if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){  
    printf("connect() error\n"); 
    exit(1); 
    } 



    // 向服務器發送數據


    printf("send message to server:");

        fgets(sendM,100,stdin);
    int send_le;
        send_le=strlen(sendM);
    sendM[send_le-1]='\0';





        send(fd,sendM,strlen(sendM),0); 

    // 從服務器接收數據
    if ((numbytes=recv(fd,receiveM,MAXDATASIZE,0)) == -1){ 
    printf("recv() error\n"); 
    exit(1); 
    } 

        printf("receive message from server:%s\n",receiveM);

    close(fd); 


 }

server.c線程

    #include <stdio.h>   
    #include <string.h>         
    #include <strings.h>  
    #include <stdlib.h>       
    #include <unistd.h>          
    #include <sys/types.h> 
    #include <sys/socket.h> 
    #include <netinet/in.h> 
    #include <arpa/inet.h>
    #include<pthread.h>
    #define PORT 1234 
    #define BACKLOG 1 





void *start_routine( void *ptr) 
{
    int fd = *(int *)ptr;
    char buf[100];
        int numbytes;
    int i,c=0;
    printf("this is a new thread,you got connected\n");
    printf("fd=%d\n",fd);


    if ((numbytes=recv(fd,buf,100,0)) == -1){ 
    printf("recv() error\n"); 
    exit(1); 
    } 

    char str[numbytes];
    char buffer[numbytes];

    //將收到的字符串反轉
    for(c=0;c<(numbytes-1);c++)
    {
    buffer[c]=buf[c];
    }



        printf("receive message:%s\n",buf);

    printf("numbytes=%d\n",numbytes);

    for(i=0;i<numbytes;i++)
    {
        str[i]=buf[numbytes-1-i];

    }


    printf("server will send:%s\n",str);
    numbytes=send(fd,str,sizeof(str),0); 
    printf("send numbytes=%d\n",numbytes);
    close(fd);
}


int  main()  
{ 

    int listenfd, connectfd;    
    struct sockaddr_in server; 
    struct sockaddr_in client;      
    int sin_size; 
    sin_size=sizeof(struct sockaddr_in); 



    pthread_t thread; //定義一個線程號

    if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
    {   
        perror("Creating socket failed.");
        exit(1);
    }


    int opt = SO_REUSEADDR;
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    bzero(&server,sizeof(server));  

    server.sin_family=AF_INET; 
    server.sin_port=htons(PORT); 
    server.sin_addr.s_addr = htonl (INADDR_ANY); 

    // 綁定
    if (bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) { 
    perror("Bind error.");
    exit(1); 
    }   

    // 監聽 
    if(listen(listenfd,BACKLOG) == -1){  /* calls listen() */ 
    perror("listen() error\n"); 
    exit(1); 
    } 

    while(1)
    {
        // accept 
        if ((connectfd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1) {
        perror("accept() error\n"); 
        exit(1); 
        }



        printf("You got a connection from %s\n",inet_ntoa(client.sin_addr) ); 


        pthread_create(&thread,NULL,start_routine,(void *)&connectfd);

    }
    close(listenfd);

}

代碼從U盤複製到home後,編譯執行3d

打開三個中端code

 

能夠發現用戶端和服務器端鏈接成功,即用戶端發送的信息,服務器端能夠接收的到。server

相關文章
相關標籤/搜索