Linux网络编程-相互聊天

2023-12-15 05:14:30

Linux网络编程

服务端

#include<stdio.h>
#include<sys/types.h>          /* See NOTES */
#include<sys/socket.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>

#define size 10

int main(int argc,char** argv)
{
    int s_fd=0;
    int c_fd=0;
    int n_read=0;
    int n_write=0;
    char readBuf[128]={0};
    char sendBuf[128]={0};  
    struct sockaddr_in s_addr;
    struct sockaddr_in c_addr;
    memset(&s_addr,'\0',sizeof(struct sockaddr_in));
    memset(&c_addr,'\0',sizeof(struct sockaddr_in));
    if(argc!=3)
    {
        printf("param is no good\n");
        exit(-1);
    }
    //1.socket
    s_fd=socket(AF_INET,SOCK_STREAM,0);
    if(s_fd==-1)
    {
        perror("socket");
        exit(-1);
    }   
    //2.bind
    s_addr.sin_family=AF_INET;
    s_addr.sin_port=htons(atoi(argv[2]));
    inet_aton(argv[1],&s_addr.sin_addr);
    if((bind(s_fd,(struct sockaddr*)&s_addr,sizeof(struct sockaddr_in)))==-1)
    {
        perror("bind");
        exit(-1);
    }
    //3.listen
    if((listen(s_fd,size))==-1)
    {
        perror("bind");
        exit(-1); 
    }
    //4.accept
    int len=sizeof(struct sockaddr_in);
    while(1)
    {
        c_fd=accept(s_fd,(struct sockaddr*)&c_addr,&len);
        if(c_fd==-1)
        {
            perror("accept");
            exit(-1);
        }
        printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
        //5.write
        if(fork()==0)  //子进程发送数据
        {
            while(1)
            {
                memset(sendBuf,'\0',sizeof(sendBuf));
                printf("input:");
                fgets(sendBuf,sizeof(sendBuf),stdin);
                n_write=write(c_fd,sendBuf,strlen(sendBuf));
                if(n_write==-1)
                {
                    perror("write");
                }
            }    
        }
        //6.read
        while(1)    //主进程接收数据
        {
            memset(readBuf,'\0',sizeof(readBuf));
            n_read=read(c_fd,readBuf,sizeof(readBuf)); 
            if(n_read==-1)
            {
                perror("read");
            }
            else
            {
                printf("%s",readBuf);
                memset(readBuf,'\0',sizeof(readBuf));
                sleep(1);
            }
        }
    }
    //7.close
    close(s_fd);
    close(c_fd);
    return  0;
}

客户端

#include<stdio.h>
#include<sys/types.h>          /* See NOTES */
#include<sys/socket.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>

int main(int argc,char** argv)
{
    int c_fd=0;
    int n_write=0;
    int n_read=0;
    char readbuf[128]={0};
    char sendbuf[128]={0};
    struct sockaddr_in c_addr;
    memset(&c_addr,'\0',sizeof(struct sockaddr_in));
    if(argc!=3)
    {
        printf("argc is no good\n");
        exit(-1);
    }
    //1.socket
    c_fd=socket(AF_INET,SOCK_STREAM,0);
    if(c_fd==-1)
    {
        perror("socket");
        exit(-1);
    }
    c_addr.sin_family=AF_INET;
    c_addr.sin_port=htons(atoi(argv[2]));
    inet_aton(argv[1],&c_addr.sin_addr);
    //2.connect
    if(connect(c_fd,(struct sockaddr*)&c_addr,sizeof(struct sockaddr_in))==-1)
    {
        perror("connect");
        exit(-1);
    }
    //3.write/read
    while(1)
    {
        if(fork()==0)  //子进程发送数据
        {
            while(1)
            {
                memset(sendbuf,'\0',sizeof(sendbuf));
                printf("input:");
                fgets(sendbuf,sizeof(sendbuf),stdin);
                n_write=write(c_fd,sendbuf,strlen(sendbuf));
                if(n_write==-1)
                {
                   perror("write");
                }
            }
        }
        n_read=read(c_fd,readbuf,sizeof(readbuf));  //主进程接收数据
        if(n_read==-1)
        {
            perror("read");
        }
        else
        {
            printf("%s",readbuf);
            memset(readbuf,0,sizeof(readbuf));
            sleep(1);
        }
    }
    //4.close
    close(c_fd);
    return 0;
}

结合多线程

服务端

#include<stdio.h>
#include<sys/types.h>          /* See NOTES */
#include<sys/socket.h>
#include<stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include<pthread.h>

int c_fd;
char msg[128]={0};
char buf[128]={0};
int n_read;

void* func1()
{
	//5.write
	while(1)
	{
		memset(msg,0,sizeof(msg));
		printf("input: ");
		gets(msg);
		write(c_fd,msg,strlen(msg));
	}

}


void* func2()
{
	//6.read
	while(1)
	{
		memset(buf,0,sizeof(buf));
		n_read=read(c_fd,buf,sizeof(buf));
		if(n_read==-1)
		{
			perror("read");
		}
		else
		{
			printf("read client:%s\n",buf);
		}
	}


}


int main(int argc,char **argv)
{
	int s_fd;
	int ret=0;
	pthread_t t1; 
	pthread_t t2;
	//	char *msg="I get the msg";

	struct sockaddr_in s_addr; 
	struct sockaddr_in c_addr; 

	memset(&s_addr,0,sizeof(struct sockaddr_in));
	memset(&c_addr,0,sizeof(struct sockaddr_in));

	if(argc!=3)
	{
		printf("params no ok\n");
		exit(-1);
	}

	//1.socket
	s_fd=socket(AF_INET,SOCK_STREAM,0);
	if(s_fd==-1)
	{
		perror("socket");
		exit(-1);
	}  
	//2.bind 
	s_addr.sin_family=AF_INET;
	s_addr.sin_port=htons(atoi(argv[2]));    
	inet_aton(argv[1],&s_addr.sin_addr);        

	bind(s_fd,(struct sockaddr*)&s_addr,sizeof(struct sockaddr_in)); 

	//3.listen
	listen(s_fd,10);


	int len=sizeof(struct sockaddr_in);
	while(1)
	{
		//4.accept
		c_fd=accept(s_fd,(struct sockaddr*)&c_addr,&len);    
		if(c_fd==-1)
		{
			perror("accept");
		}    

		printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));


		pthread_create(&t1,NULL,func1,NULL);

		pthread_create(&t2,NULL,func2,NULL);    
	}
	//7.close
	close(s_fd);

	return 0;
}

客户端

#include<stdio.h>
#include<sys/types.h>          /* See NOTES */
#include<sys/socket.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>

int c_fd;
int n_read;
char buf[128]={0};
char msg[128]={0};



void*  func1()
{
	//4.send
	while(1)
	{
		memset(msg,0,sizeof(msg));
		printf("input: ");
		gets(msg);
		write(c_fd,msg,strlen(msg));
	}

}


void* func2()
{
	//5.read
	while(1)
	{
		memset(buf,0,sizeof(buf));
		n_read=read(c_fd,buf,sizeof(buf));
		if(n_read==-1)
		{
			perror("read");
		}
		else
		{
			printf("read sever:%s\n",buf);
		}
	}

}

int main(int argc,char **argv)
{
	//	char *msg="msg from client";

	pthread_t t1; 
	pthread_t t2;        


	struct sockaddr_in c_addr; 

	memset(&c_addr,0,sizeof(struct sockaddr_in));

	if(argc!=3)
	{
		printf("params is no ok\n");
		exit(-1);
	}

	//1.socket
	c_fd=socket(AF_INET,SOCK_STREAM,0);
	if(c_fd==-1)
	{
		perror("socket");
		exit(-1);
	}  
	//2.bind 
	c_addr.sin_family=AF_INET;
	c_addr.sin_port=htons(atoi(argv[2]));    
	inet_aton(argv[1],&c_addr.sin_addr);        

	//3.connect
	if(connect(c_fd,(struct sockaddr*)&c_addr,sizeof(struct sockaddr_in))==-1)
	{
		perror("connect");
		exit(-1);
	}   


	pthread_create(&t1,NULL,func1,NULL); 
	while(1)
	{         

		pthread_create(&t2,NULL,func2,NULL);
	}

	//6.close
	close(c_fd);

	return 0;
}

文章来源:https://blog.csdn.net/qq_47944751/article/details/135006525
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。