zoukankan      html  css  js  c++  java
  • 【Linux/unix网络编程】之使用socket进行TCP编程

    实验一 TCP数据发送与接收

    【实验目的】

    1、熟练掌握套接字函数的使用方法。

    2、应用套接字函数完成基本TCP通讯,实现服务器与客户端的信息交互。

    【实验学时】

        4学时

    【实验内容】

    实现一个服务器与一个客户之间通讯。具体功能如下:

    (1)服务器端:

    服务器端等待客户的连接,一旦连接成功,则显示客户的IP地址、端口号;

    循环接收客户发来的信息并在终端上显示,同时在信息前加入序号并返回给客户端;当从客户接收到bye后不再发送给各户并退出程序。

    (2)客户端:

    根据用户从终端输入的服务器IP地址及端口号连接到相应的服务器;

    连接成功后,循环从终端输入信息,并将信息发送给服务器,再从服务器接收信息,并显示在终端上。

    当从终端输入bye并发送给服务器后,程序退出。

    程序实现:

    服务器端:

     1 /*     TcpServer.c  
     2      copyright@msxh 2015/09/21
     3  */
     4 #include <stdio.h>
     5 #include <string.h>
     6 #include <sys/socket.h>
     7 #include <netinet/in.h>
     8 #include <stdlib.h>
     9 
    10 int main(){
    11 
    12     struct sockaddr_in server;
    13     struct sockaddr_in client;
    14     int listenfd,connetfd;
    15     char ip[20];
    16     int port;
    17     int addrlen;
    18     char rebuf[100];
    19     char wrbuf[100];
    20     char tmp[100];
    21     int revlen;
    22     /*---------------------socket-------------------*/
    23     if((listenfd = socket(AF_INET,SOCK_STREAM,0))== -1){
    24         perror("socket() error
    ");
    25         exit(1);
    26     }
    27 
    28     /*----------------------IO-----------------------*/
    29     printf("Please input the ip:
    ");
    30     scanf("%s",ip);
    31     printf("Please input the port:
    ");
    32     scanf("%d",&port);
    33 
    34     /*---------------------bind----------------------*/
    35     bzero(&server,sizeof(server));
    36     server.sin_family = AF_INET;
    37     server.sin_port = htons(port);
    38     server.sin_addr.s_addr = inet_addr(ip);
    39     if(bind(listenfd,(struct sockaddr *)&server,sizeof(server))== -1){
    40         perror("bind() error
    ");
    41         exit(1);
    42     }
    43 
    44     /*----------------------listen-------------------*/
    45     if (listen(listenfd,5)== -1){
    46         perror("listen() error
    ");
    47         exit(1);
    48     }
    49 
    50     /*----------------------accept------------------*/
    51     addrlen = sizeof(client);
    52     if((connetfd = accept(listenfd,(struct sockaddr *)&client,&addrlen))== -1){
    53         perror("accept() error
    ");
    54         exit(1);
    55     }
    56     /*---------------------show client---------------*/
    57     printf("connect successful!
    ");
    58     printf("the client ip is %s,port is %d
    ",inet_ntoa(client.sin_addr),ntohs(port));
    59 
    60     /*----------------------read and write----------*/
    61     int serial = 0;
    62     while(1){
    63     bzero(rebuf,sizeof(rebuf));
    64     revlen = read(connetfd,rebuf,sizeof(rebuf));
    65     if((memcmp("bye",rebuf,3))== 0){
    66         printf("Bye-bye then close the connect...
    ");
    67         break;
    68     }
    69     bzero(wrbuf,sizeof(wrbuf));
    70     bzero(tmp,sizeof(tmp));
    71     sprintf(tmp,"%d",serial);
    72     strcat(tmp,rebuf);
    73     bcopy(tmp,wrbuf,strlen(tmp));
    74     write(connetfd,wrbuf,sizeof(wrbuf));
    75     rebuf[revlen] = '';
    76     printf("the info from client is:%s
    ",rebuf);
    77     serial++;
    78     }
    79 
    80     /*----------------------close-------------------*/
    81     close(connetfd);
    82     close(listenfd);
    83 
    84     return 0;
    85 }

    客户端实现:

    /*       TcpClient.c
         copyright@msxh 2015/09/21
     */
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <stdlib.h>
    
    int main(){
        int sockfd;
        char wrbuf[100];
        char ip[20];
        int port;
        int revlen;
        char rebuf[100];
        struct sockaddr_in server;
    
        /*---------------------socket---------------------*/
        if((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1){
            perror("socket error
    ");
            exit(1);
        }
    
        /*---------------------connect--------------------*/
        printf("Please input the ip:
    ");
        scanf("%s",ip);
        printf("Please input the port:
    ");
        scanf("%d",&port);
        bzero(&server,sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons(port);
        inet_aton(ip,&server.sin_addr);
        if(connect(sockfd,(struct sockaddr *)&server,sizeof(server))== -1){
            perror("connect() error
    ");
            exit(1);
        }
    
        /*-----------------------read and write------------------*/
        while(1){
        bzero(wrbuf,sizeof(wrbuf));
        bzero(rebuf,sizeof(rebuf));    
        printf("Please input the info:
    ");
        scanf("%s",wrbuf);
        if((memcmp("bye",wrbuf,3))== 0){
            write(sockfd,wrbuf,strlen(wrbuf));
            printf("Bye-bye then close the connect...
    ");
            break;
        }
        //printf("%s
    ",wrbuf);
        write(sockfd,wrbuf,strlen(wrbuf));
        revlen = read(sockfd,rebuf,sizeof(rebuf));
        rebuf[revlen] = '';
        printf("The info from server is: %s
    ",rebuf);
        }
        /*------------------------close--------------------------*/
        close(sockfd);
    
        return 0;
    }

    makefile文件:

    main:tcpserver.c tcpclient.c
        gcc -o tcpserver tcpserver.c
        gcc -o tcpclient tcpclient.c

    学习Linux、Unix网络编程时写的第一个程序。。。

  • 相关阅读:
    如何规范自己的编程以及软件开发目录(二)
    关于README的内容
    关于编程编程规范以及开发目录的规范性
    第五章:条件、循环以及其他语句(上)
    第四章 当索引行不通时
    python-zipfile模块
    python-shutil模块
    python-sys模块
    python-os模块
    python-threading.Thread(多线程)
  • 原文地址:https://www.cnblogs.com/msxh/p/4900728.html
Copyright © 2011-2022 走看看