Linux学习笔记(C语言):tcp实现客户端与服务器端聊天

客户端

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>

typedef struct sockaddr*(SA);

int main(int argc, const char *argv[])
{
	int conn = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == conn)
	{
		perror("conn error!\n");
		exit(1);
	}
	
	struct sockaddr_in ser;
	bzero(&ser,sizeof(ser));
	ser.sin_family = AF_INET;
	ser.sin_port = htons(50000);
	ser.sin_addr.s_addr = inet_addr("192.168.1.139");
	int ret = connect(conn,(SA)&ser,sizeof(ser));
	if(-1 == ret)
	{
		perror("connect error!\n");
		exit(1);
	}
	char buf[1024] = {0};
	pid_t pid = fork();
	if(pid > 0)
	{
		while(1)
		{
			printf("to ser:");
			fflush(stdout);
			bzero(buf,sizeof(buf));
			fgets(buf,sizeof(buf),stdin);
			send(conn,buf,strlen(buf)+1,0);
			if(0 == strcmp(buf,"#quit\n"))
			{
				kill(pid,9);
				exit(0);
			}
		}
	}
	else if(0 == pid)
	{
		while(1)
		{
			bzero(buf,sizeof(buf));
			int rd = recv(conn,buf,sizeof(buf),0);
			if(0 == strcmp(buf,"#quit\n"))
			{
				kill(getppid(),9);
				exit(0);
			}
			printf("from ser:%s\n",buf);
		}
	}
	else
	{
		perror("fork error!\n");
		exit(1);
	}
	close(conn);
	return 0;
}

服务器端

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>

typedef struct sockaddr*(SA);

int main(int argc, const char *argv[])
{
	int listfd = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == listfd)
	{
		perror("socket error!\n");
		exit(1);
	}
	
	struct sockaddr_in ser,cli;
	bzero(&ser,sizeof(ser));
	bzero(&cli,sizeof(cli));
	ser.sin_family = AF_INET;
	ser.sin_port = htons(50000);
	ser.sin_addr.s_addr = INADDR_ANY;
	int ret = bind(listfd,(SA)&ser,sizeof(ser));
	if(-1 == ret)
	{
		perror("bind error!\n");
		exit(1);
	}
	listen(listfd,3);
	int len = sizeof(cli);
	int conn = accept(listfd,(SA)&cli,&len);
	if(-1 == conn)
	{
		perror("accept error!\n");
		exit(1);
	}

	char buf[1024] = {0};
	pid_t pid = fork();
	if(pid > 0)
	{
		while(1)
		{
			printf("to cli:");
			fflush(stdout);
			bzero(buf,sizeof(buf));
			fgets(buf,sizeof(buf),stdin);
			send(conn,buf,strlen(buf)+1,0);
			if(0 == strcmp(buf,"#quit\n"))
			{
				kill(pid,9);
				exit(0);
			}
		}
	}
	else if(0 == pid)
	{
		while(1)
		{
			bzero(buf,sizeof(buf));
			int rd = recv(conn,buf,sizeof(buf),0);
			if(0 == strcmp(buf,"#quit\n"))
			{
				kill(getppid(),9);
				break;
			}
			printf("from cli:%s",buf);
		}
	}
	else
	{
		perror("fork error!\n");
		exit(1);
	}

	close(listfd);
	close(conn);
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值