关于linux下的TCP 的socket server和client,我的理解

CSDN的博客中有这样一篇博客点击打开链接,是关于TCP的socket编程的,我想基于我的理解解释一下,毕竟原文只给出了纯粹的代码,


我们知道上图是它们之间的工作模式,基本编程的规则也是按照上面来的

下面是服务器端的代码,但插入的代码本应该是C的,但是CSDN中插入的代码选项却没有C的,比较让人困扰

#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h> 
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <strings.h>
#include <ctype.h> 
             
char host_name[20];
int port = 8000;

int main()
{   
        
        struct sockaddr_in sin,pin;   //以太网套接字的地址结构,是协议族AF_INET对应的结构
        int sock_descriptor,temp_sock_descriptor,address_size;
        int i , len , on=1;
        char buf[16384];
    
        sock_descriptor = socket(AF_INET,SOCK_STREAM,0);//通过socket(int domain,int type,int protocol)的函数原型创建AF_INIT协议族的流//类型的socket,当然type除了流socket的之外还有SOCKET_RAW,据说这种类型的socket甚至可以伪装数据包,如果阅读这篇文章的大神对其很了解的话,希望能够私信//发给我关于它的资料或网址。
        bzero(&sin,sizeof(sin));//将变量sin置0
        sin.sin_family = AF_INET; //指定协议族
        sin.sin_addr.s_addr = INADDR_ANY;
        sin.sin_port = htons(port);//地址结构的端口地址,port是主机字节序,通过htons()进行字节序转换成网络字节序
        if(bind(sock_descriptor,(struct sockaddr *)&sin,sizeof(sin)) == -1)//将sin的地址和socket文件描述符绑定到一起,绑定是数据接收和发//送的前提
        {
                perror("call to bind");
                exit(1);
        }
        if(listen(sock_descriptor,100) == -1)//监听用来初始化服务器可连接队列,因为一次只能处理一个连接请求,当收到多个请求,将会存储在队列中,///先到先得
        {
                perror("call to listem");
                exit(1);
        }
        printf("Accpting connections...\n");

        while(1)
        {
                address_size = sizeof(pin);
                temp_sock_descriptor = accept(sock_descriptor,(struct sockaddr *)&pin,&address_size);//前面提到,当收到多个连接请求时//后面未处理请求会被排在队列中,而accept()返回一个新的socket文件描述符来表示来自客户端的连接
                if(temp_sock_descriptor == -1)
                {
                        perror("call to accept");
                        exit(1);
                }
                if(recv(temp_sock_descriptor,buf,16384,0) == -1)//该描述符是接收端的套接字,buf是用来存储接收到的数据,16384是buf的长度
                {
                        perror("call to recv");
                        exit(1);
                }
                inet_ntop(AF_INET,&pin.sin_addr,host_name,sizeof(host_name));
                printf("received from client(%s):%s\n",host_name,buf);

                len = strlen(buf);
                for(i = 0 ; i  < len ; i++)
                {
                        buf[i] =  toupper(buf[i]);
                }

                if(send(temp_sock_descriptor,buf,len+1,0) == -1)
                {
                        perror("call to send");
                        exit(1);

                }
                close(temp_sock_descriptor);


        }



}
客户端的代码如下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <strings.h>
#include <ctype.h>


char * host_name = "127.0.0.1";
int port = 8000;

int main(int argc , char * argv[])
{
        char buf[8192];
        //char message[256];
        int socket_descriptor;
        struct sockaddr_in pin;
        char * str ="A default test string";
        if(argc < 2)
        {
                printf("we will send a default test string.\n");

        }
        else
        {
                str = argv[1];
                if(argc == 3)
                {
                        host_name = argv[2];
                }
        }

        bzero(&pin,sizeof(pin));
        pin.sin_family = AF_INET;
        inet_pton(AF_INET,host_name,&pin.sin_addr);
        pin.sin_port = htons(port);
        if((socket_descriptor =  socket(AF_INET,SOCK_STREAM,0)) == -1)//与服务器端相对应
        {
                perror("error opening socket \n");
                exit(1);
        }
        if(connect(socket_descriptor,(struct sockaddr * )&pin,sizeof(pin)) == -1)客户端在建立起套接字之后,不再需要进行地址绑定,可通过connect函数直接连接服务器
        {
                perror("error connecting to socket \n");
                exit(1);
        }

        printf("sending message %s to server ..\n",str);
        if( write(socket_descriptor,str,strlen(str)+1) == -1 )//将string写入socket描述符中
        {
                perror("error in send \n");
                exit(1);
        }

        printf("..sent message ...wait for message..\n");
        if( read(socket_descriptor,buf,8192) == -1 )//从socket描述符中将8192个字符读到buf中
        {
                perror("error in receiving response from server \n");
                exit(1);
        }

        printf("\nResponse from server:\n\n%s\n",buf);
        close(socket_descriptor);
        return 1;


}
总之,大致过程应该是这样子的,服务器端在经过建立socket,绑定和监听之后,便一直等待客户端建立socket之后所发出的连接请求,客户端写数据,服务器端收到数据,或者服务器端发送数据,客户端读取数据。我对linux的网络编程还在皮毛阶段,有大神知道的比我多的,还望不吝赐教。拜托了,atohayorosikune
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值