zoukankan      html  css  js  c++  java
  • linux下tcp #c编程

    **

    一、工具准备

    **

    1. windows下linux子系统(在我的博客里面有相关搭建教程)
    2. vscode(自行下载安装)

    二、创建文件和文件夹

    选择你想要放代码的地方然后

    mkdir classpro   #创建文件夹
    cd classpro      #进入文件夹
    touch tcpclient.c      #创建tcp客户端
    touch tcpserver.c      #创建tcp服务端
    

    三、编程

    tcp客户端:

    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <strings.h>
    int main(int argc,char **argv)
    {
    	int client_fd;
    	int ret;
    	int count;
    	struct sockaddr_in server_addr;
    	char buf[512];
    	char recv_buf[512];
    	int sock_size=sizeof(struct sockaddr_in);
    	if(argc<2)
    	{
    		printf("Usage:./client serverip
    ");
    		return 0;
    	}
    	bzero(&server_addr,sock_size);
    	client_fd=socket(AF_INET,SOCK_STREAM,0);
    	server_addr.sin_family=AF_INET;
    	server_addr.sin_port=htons(8000);
    	server_addr.sin_addr.s_addr=inet_addr(argv[1]);
    	ret=connect(client_fd,(struct sockaddr*)&server_addr,sock_size);
    	if(ret<0)
    	{
    		perror("Failed to connect:");
    		return -1;
    	}
    	printf("Connect successfully!
    ");
    	while(1)
    	{
    		fgets(buf,512,stdin);
    		ret=write(client_fd,buf,strlen(buf));//客户端发送给服务器
    		if(ret<=0)
    			break;
    		if(strncmp(buf,"quit",4)==0)
    			break;	
    		count=read(client_fd,recv_buf,511);  //读取服务器发送的消息
    		if(count>0)
    		{
    			recv_buf[count]=0;
    			printf("Echo:%s
    ",recv_buf);   
    		}	
    		else
    		{
    			break;
    		}
    	}
    	close(client_fd);
    	return 0;
    	
    }
    

    tcp服务端:(这里使用了多线程)

    #include <stdio.h>  //创建tcp服务端像手机app发送消息
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <strings.h>
    #include <pthread.h>
    #define MAXCONN 100
    struct session_message //服务端ip地址结构体
    {
    int comm_fd;//请求号
    char ipaddr[16]//请求地址
    };
    void *session_thread(void *arg)//线程函数
    {
    struct session_message *p=(struct session_message*)arg;
    int comm_fd=p->comm_fd;//取出结构体ip地址
    char ipaddr[16];
    sprintf(ipaddr,"%s",p->ipaddr);//打印连接的客户端ip
    //free(p);//释放指针空间
    while(1)
    {
    char buff[512];
    int count;
    count=read(comm_fd,buff,511);
    if(count>0)
    {
    buff[count]=0;
    printf("a string from %s:%s
    ",ipaddr,buff);//打印出ip地址和接收的消息
    if(strncmp(buff,"quit",4)==0)//接收到quit退出
    {
    printf("a talking is over!
    ");
    break;
    }
    write(comm_fd,buff,count);
    }
    else
    {
    printf("a talking is over@
    ");
    break;
    }
    }
    }
    int main()
    {
      
       int listen_fd,comm_fd;
    	int ret;
    	int i=1;
    	struct sockaddr_in server_addr,client_addr;
    	int sock_size=sizeof(struct sockaddr_in);
    	listen_fd=socket(AF_INET,SOCK_STREAM,0);
    	if(listen_fd<0)
    	{
    		perror("Failed to create socket:");
    		return -1;
    	}
    	bzero(&server_addr,sock_size);
    	server_addr.sin_family=AF_INET;
    	server_addr.sin_port=htons(8000);
    	server_addr.sin_addr.s_addr=INADDR_ANY;
    	setsockopt(listen_fd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(int));
    	ret=bind(listen_fd,(struct sockaddr*)&server_addr,sock_size);
    	if(ret==0)
    	{
    		printf("Bind Successfully!
    ");
    	}
    	ret=listen(listen_fd,MAXCONN);
    	if(ret==0)
    	{
    		printf("Listen Successfully!
    ");
    	}
    while((comm_fd=accept(listen_fd,(struct sockaddr*)&client_addr,&sock_size))>=0)
    {
       pthread_t tid;
       struct session_message msg;
        msg.comm_fd=comm_fd;
    	inet_ntop(AF_INET,&client_addr.sin_addr.s_addr,msg.ipaddr,16);
       pthread_create(&tid,NULL,session_thread,((void*)&msg));//创建线程
    
    }
    close(listen_fd);
    return 0;
    }
    

    四、编译

    gcc tcpclient.c  -o tcpclient
    gcc tcpserver.c -o tcpserver -lpthread
    #编译完之后我们可以看到生成的新的两个二进制文件
    #我们执行它
    server端执行:./tcpserver 你的ip地址
    cliect端执行:./tcpcliet 服务器ip 你的ip
    #不搞两个的页可以用网络调试助手自己的进行调试
    

    自此简单的tcp开发结束。做个笔记而已,高手勿喷。

    一键三连呀!
  • 相关阅读:
    16.ajax_case08
    16.ajax_case07
    16.ajax_case06
    16.ajax_case05
    9.2 sun.py
    9.1 mongo_python.py
    pandas-如何得到某一个值所在的行
    [已解决]ValueError: row index was 65536, not allowed by .xls format
    [已解决]python FileNotFoundError: [WinError 3] for getsize(filepath)
    Tomcat6连接数设置:permsize
  • 原文地址:https://www.cnblogs.com/jee-cai/p/14095356.html
Copyright © 2011-2022 走看看