【Linux/unix網絡編程】之使用socket進行TCP編程

實驗一 TCP數據發送與接收編程

【實驗目的】服務器

一、熟練掌握套接字函數的使用方法。網絡

二、應用套接字函數完成基本TCP通信,實現服務器與客戶端的信息交互。併發

【實驗學時】socket

    4學時tcp

【實驗內容】函數

實現一個服務器與一個客戶之間通信。具體功能以下:學習

(1)服務器端:spa

服務器端等待客戶的鏈接,一旦鏈接成功,則顯示客戶的IP地址、端口號;code

循環接收客戶發來的信息並在終端上顯示,同時在信息前加入序號並返回給客戶端;當從客戶接收到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\n");
25         exit(1);
26     }
27 
28     /*----------------------IO-----------------------*/
29     printf("Please input the ip:\n");
30     scanf("%s",ip);
31     printf("Please input the port:\n");
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\n");
41         exit(1);
42     }
43 
44     /*----------------------listen-------------------*/
45     if (listen(listenfd,5)== -1){
46         perror("listen() error\n");
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\n");
54         exit(1);
55     }
56     /*---------------------show client---------------*/
57     printf("connect successful!\n");
58     printf("the client ip is %s,port is %d\n",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...\n");
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] = '\0';
76     printf("the info from client is:%s\n",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\n");
        exit(1);
    }

    /*---------------------connect--------------------*/
    printf("Please input the ip:\n");
    scanf("%s",ip);
    printf("Please input the port:\n");
    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\n");
        exit(1);
    }

    /*-----------------------read and write------------------*/
    while(1){
    bzero(wrbuf,sizeof(wrbuf));
    bzero(rebuf,sizeof(rebuf));    
    printf("Please input the info:\n");
    scanf("%s",wrbuf);
    if((memcmp("bye",wrbuf,3))== 0){
        write(sockfd,wrbuf,strlen(wrbuf));
        printf("Bye-bye then close the connect...\n");
        break;
    }
    //printf("%s\n",wrbuf);
    write(sockfd,wrbuf,strlen(wrbuf));
    revlen = read(sockfd,rebuf,sizeof(rebuf));
    rebuf[revlen] = '\0';
    printf("The info from server is: %s\n",rebuf);
    }
    /*------------------------close--------------------------*/
    close(sockfd);

    return 0;
}

 

makefile文件:

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

學習Linux、Unix網絡編程時寫的第一個程序。。。

相關文章
相關標籤/搜索