网络编程 day2 5.16

该代码示例展示了如何使用C语言实现一个简单的TCP/IP通信。服务器端创建套接字,绑定IP和端口,监听连接,接收并回应客户端消息。客户端则创建连接,发送消息并接收服务器的响应。程序在遇到错误时提供行号和错误信息。
摘要由CSDN通过智能技术生成

服务端

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

#define IP "192.168.10.10"
#define PORT 8888
int main(int argc, const char *argv[])
{
	//创建
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		printf("__%d__",__LINE__);
		perror("socket");
		return -1;
	}
	printf("sfd = %d\n",sfd);

	//绑定
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
	if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) < 0)
	{
		printf("__%d__",__LINE__);
		perror("bind");
		return -1;
	}
	printf("绑定成功\n");

	//监听
	if(listen(sfd,128) < 0)
	{
		printf("__%d__",__LINE__);
		perror("listen");
		return -1;
	}
	printf("监听成功\n");

	//获取新的套接字
	struct sockaddr_in cin;
	socklen_t len = sizeof(cin);
	int newfd = accept(sfd,(struct sockaddr *)&cin,&len);
	if(newfd < 0)
	{
		printf("__%d__",__LINE__);
		perror("accept");
		return -1;
	}
	//接收
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		res = recv(newfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			printf("__%d__",__LINE__);
			perror("recv");
			return -1;
		}
		else if(0 == res)
		{
			printf("客户端已下线");
			break;
		}
		printf("接受成功\n");

		//发送
		scanf("%s",buf);
		if(send(newfd,buf,sizeof(buf),0) < 0)
		{
			printf("__%d__",__LINE__);
			perror("send");
			return -1;
		}
		printf("发送成功");

	}
	//关闭
	if(close(sfd) < 0)
	{
		printf("__%d__",__LINE__);
		perror("close");
		return -1;
	}
	if(close(newfd) < 0)
	{
		printf("__%d__",__LINE__);
		perror("close");
		return -1;
	}
	return 0;
}

客户端

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

#define IP "192.168.10.10"
#define PORT 6666
int main(int argc, const char *argv[])
{
	//创建
	int cfd = socket(AF_INET,SOCK_STREAM,0);
	if(cfd < 0)
	{
		printf("__%d__",__LINE__);
		perror("socket");
		return -1;
	}
	printf("cfd = %d\n",cfd);

	//连接
	struct sockaddr_in cin;
	cin.sin_family = AF_INET;
	cin.sin_port = htons(PORT);
	cin.sin_addr.s_addr = inet_addr(IP);
	if(connect(cfd,(struct sockaddr *)&cin,sizeof(cin)) < 0)
	{
		printf("__%d__",__LINE__);
		perror("connect");
		return -1;
	}
	printf("连接成功");


	//接收
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{
		bzero(buf,sizeof(buf));
	
		//发送
		scanf("%s",buf);
		if(send(cfd,buf,sizeof(buf),0) < 0)
		{
			printf("__%d__",__LINE__);
			perror("send");
			return -1;
		}
		printf("发送成功");
		res = recv(cfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			printf("__%d__",__LINE__);
			perror("recv");
			return -1;
		}
		else if(0 == res)
		{
			printf("服务端已下线");
			break;
		}
		printf("%s\n",buf);

	}
	//关闭
	if(close(cfd) < 0)
	{
		printf("__%d__",__LINE__);
		perror("close");
		return -1;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值