网络编程:完成TCP服务器 完成TCP客户端

 服务器

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8888 //端口号的网络字节序 1024~49151
#define IP "192.168.10.236"//ifconfig查看本机IP
#define ERR_MSG(msg)do{\
	printf("line:%d",__LINE__);\
	perror(msg);\
}while(0)

int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket creat success sfd = %d line = __%d__\n",sfd,__LINE__);
	//功能:允许端口快速被重复使用,快速复用
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	//填充服务器自身的地址信息结构体
	//真实的地址信息结构体根据地址族制定 AF_INET:man 7 ip
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
	//将IP地址和端口号绑定到指定的套接字文件描述符上
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success lin = %d\n",__LINE__);
	//监听
	if(listen(sfd,128)<0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success line = %d\n",__LINE__);
	//获取新的套接字
	struct sockaddr_in cin;
	socklen_t len = sizeof(cin);
	//
	int newfd = accept(sfd,(struct sockaddr*)&cin,&len);
	if(newfd < 0)
	{
		ERR_MSG("accept");
		return -1;
	}
	printf("[%s:%d] 客户端连接成功 newfd = %d  line = %d\n",\
			inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
	//接收
	ssize_t res = 0;
	char buf[128] = "";
	while(1)
	{
		bzero(buf,sizeof(buf));	
		res = recv(newfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if(0 == res)
		{
			printf("[%s:%d] 客户端下线 newfd = %d  line = %d\n",\
			inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
		}
		printf("接收成功[%s:%d] newfd = %d:%s, line = %d\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);

	//发送
	printf("请输入要发送的信息:");
	scanf("%s",buf);
	if(send(newfd,buf,sizeof(buf),0)<0)
	{
		ERR_MSG("send");
		return -1;
	}
	printf("发送成功\n");
	}
	if(close(newfd)<0)
	{
		ERR_MSG("close");
		return -1;
	}
	if(close(sfd)<0)
	{
		ERR_MSG("close");
		return -1;
	}
	return 0;
}

客户端

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8888 //端口号的网络字节序 1024~49151
#define IP "192.168.10.236"//ifconfig查看本机IP
#define ERR_MSG(msg)do{\
	printf("line:%d",__LINE__);\
	perror(msg);\
}while(0)

int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("socket creat success sfd = %d line = __%d__\n",sfd,__LINE__);
	//功能:允许端口快速被重复使用,快速复用
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	//填充服务器自身的地址信息结构体
	//真实的地址信息结构体根据地址族制定 AF_INET:man 7 ip
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
	//将IP地址和端口号绑定到指定的套接字文件描述符上
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success lin = %d\n",__LINE__);
	//监听
	if(listen(sfd,128)<0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success line = %d\n",__LINE__);
	//获取新的套接字
	struct sockaddr_in cin;
	socklen_t len = sizeof(cin);
	//
	int newfd = accept(sfd,(struct sockaddr*)&cin,&len);
	if(newfd < 0)
	{
		ERR_MSG("accept");
		return -1;
	}
	printf("[%s:%d] 客户端连接成功 newfd = %d  line = %d\n",\
			inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
	//接收
	ssize_t res = 0;
	char buf[128] = "";
	while(1)
	{
		bzero(buf,sizeof(buf));	
		res = recv(newfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if(0 == res)
		{
			printf("[%s:%d] 客户端下线 newfd = %d  line = %d\n",\
			inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
		}
		printf("接收成功[%s:%d] newfd = %d:%s, line = %d\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);

	//发送
	printf("请输入要发送的信息:");
	scanf("%s",buf);
	if(send(newfd,buf,sizeof(buf),0)<0)
	{
		ERR_MSG("send");
		return -1;
	}
	printf("发送成功\n");
	}
	if(close(newfd)<0)
	{
		ERR_MSG("close");
		return -1;
	}
	if(close(sfd)<0)
	{
		ERR_MSG("close");
		return -1;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值