基于Linux平台使用socket实现 一个最简单的聊天系统

先看下效果 :

启动服务器端 :

启动客户端:

 

客户端向服务器端发送:i am client

服务器端收到 并回送 i am server

一直 循环 直到客户端发送 quit 退出系统

客户端发送quit的时候 服务器端也会退出 

 

 作为最基础的socket入门练习 

服务器端代码:


#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>

#define PORT 6666
#define BACKLOG 10
#define MAX_SIZE	2048




int main()
{
	int socket_fd;
	struct sockaddr_in server_addr;

	char buffer[MAX_SIZE]={0};
	char message[MAX_SIZE];
	socket_fd = socket(AF_INET,SOCK_STREAM,0);
	bzero(&server_addr,sizeof(server_addr));

	server_addr.sin_family = AF_INET;
	server_addr.sin_port=htons(PORT);
	server_addr.sin_addr.s_addr = INADDR_ANY;

	bind(socket_fd,(struct sockaddr *)&server_addr,sizeof(server_addr));

	listen(socket_fd,BACKLOG);

	printf("Server bind success \n");

	struct sockaddr_in peer_addr;
	socklen_t peer_len  = sizeof(peer_addr);
	



	while(1)
	{
		int client_fd;
		client_fd = accept(socket_fd,(struct sockaddr* )&peer_addr,&peer_len);
		printf("Receive a connect from clinet fd[%d,ip:%s,port:%d]\n",
			client_fd,inet_ntoa(peer_addr.sin_addr),ntohs(peer_addr.sin_port));
		while (1)
		{
			memset(buffer,0,sizeof(buffer));
			int recv_len = recv(client_fd,buffer,MAX_SIZE,0);
			if(recv_len == 0)
			{
				printf("Clinet is already closed\n");
				break;
			}
			else
			{
				printf("Recv len is %d \n",recv_len);
			}

			printf("Clinet says:\n");
			strcpy(message,buffer);
			fputs(message,stdout);

			memset(buffer,0,MAX_SIZE);
			printf("Please input:\n");
			fgets(buffer,sizeof(buffer),stdin);

			send(client_fd,buffer,recv_len,0);
		}

		close(client_fd);
		break;
	}
	
	close(socket_fd);

	printf("Server exit\n");
	return 0;
	
}

客户端代码:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>


#define CLIETN_PORT	6666
#define MAX_SIZE 2048


int main(int argc,char *argv[])
{
	
	if(argc != 2)
	{
		printf("Please input server ip\n");
		exit(1);
	}

	char buffer[MAX_SIZE];
	int socket_fd = socket(AF_INET,SOCK_STREAM,0);
	const char *server_ip = argv[1];

	struct sockaddr_in server_addr;

	bzero(&server_addr,sizeof(server_addr));

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(CLIETN_PORT);
	inet_pton(AF_INET,server_ip,&server_addr.sin_addr);
	connect(socket_fd,(struct sockaddr*)&server_addr,sizeof(server_addr));

	printf("Connect to server success\n");

	memset(buffer,0,MAX_SIZE);


	printf("Please input:\n");

	while (fgets(buffer,sizeof(buffer),stdin) != NULL)
	{
		if(strncmp(buffer,"quit",4) == 0)
		{
			printf("intpu  is: quit break\n");
			break;
		}
		else
		{
			printf("intpu  is: %s\n",buffer);			
		}

		send(socket_fd,buffer,strlen(buffer),0);
		memset(buffer,0,sizeof(buffer));
		recv(socket_fd,buffer,sizeof(buffer),0);
		printf("Server says:\n");
		fputs(buffer,stdout);
		memset(buffer,0,sizeof(buffer));

		printf("Please input:\n");

	}

	printf("Client will be closed\n");
	close(socket_fd);
	return 0;
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QMCY_jason

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值