网络编程 day2 c语言

该代码示例展示了如何使用C语言编写一个简单的TCP服务器(tcp_ser.c)和客户端(tcp_cli.c)。服务器监听特定端口,接收并回应客户端连接请求,客户端则连接服务器并发送数据,服务器接收到数据后回传确认信息。程序中包含了socket创建、bind、listen、accept、send和recv等关键函数的使用。
摘要由CSDN通过智能技术生成

编写一个简陋的tcp协议server与client(无fork)

注释写在服务器端了,用户端的就懒得写了

tcp_ser.c

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

#define ERR_MSG(msg) do{\
		printf("line %d\n",__LINE__);\
		perror(msg);\
		return -1;\
		}while(0)

#define SUC_MSG(msg) do{\
	printf("%s success\n",msg);\
}while(0)

#define PORT 1991

#define IP "192.168.10.23"

int main(int argc, const char *argv[])
{
	//create the socket
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0){
		ERR_MSG("socket");
	}
	
	//man 2 bind
	//man 7 ip
	//the actual structure passed for the second argument of bind method will be
	//depend on the address family
	//in this case,it will be AF_INET,aka ipv4
	//the actual one should be (after checking manual) struct sockaddr_in.
	//we define a variable in struct sockaddr_in then convert it into
	//struct sockaddr
	struct sockaddr_in sin;
	sin.sin_family         = AF_INET;       //address family
	sin.sin_port           = htons(PORT);   //port in network byte order
	sin.sin_addr.s_addr    = inet_addr(IP); //internet address(in network byte order)

	//bind the sockaddr with the sfd
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(struct sockaddr_in)) < 0){
		ERR_MSG("bind");
	}
	printf("bind success\n");

	//mark the socket sfd as the one specifically used on listening
	if(listen(sfd,128) < 0){
		ERR_MSG("listen\n");
	}
	SUC_MSG("listen");
	
	//create a new sockaddr_in structrue to store the client sock address info
	struct sockaddr_in cin;
	socklen_t len = sizeof(cin);
	//create a new file descriptor to be actually communicated with
	int newfd = accept(sfd,(struct sockaddr*)&cin,&len);
	
	if(newfd < 0){
		ERR_MSG("accept");
	}
	SUC_MSG("accept");
	
	printf("[%s:%d]Client is successfully connected. newfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);

	char buf[128];
	while(1){
		bzero(buf,sizeof(buf));
		//receive data
		int res = recv(newfd,buf,sizeof(buf),0);
		if(res < 0){
			ERR_MSG("recv");
		}
		else if(res == 0)
		{
			printf("Client offline\n");
			break;
		}

		SUC_MSG(buf);
		//send data
		strcat(buf," is received\n");
		res = send(newfd,buf,sizeof(buf),0);	
		if(res < 0){
			ERR_MSG("send");
		}
	}

	//close the socket
	if(close(sfd) < 0){
		ERR_MSG("close");
	}
	return 0;
}

tcp_cli.c

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

#define ERR_MSG(msg) do{\
	perror(msg);\
	return -1;\
}while(0)

#define SUC_MSG(msg) do{\
	printf("%s success\n",msg);\
}while(0)

#define PORT 1997

#define IP "192.168.10.23"

#define SERVERPORT 1991

#define SERVERIP "192.168.10.23"

static int *ifAlarm = NULL;

void handler(int signal){
	*ifAlarm = signal;
}

int main(int argc, const char *argv[])
{
	int myAlarm = 0;
	ifAlarm = &myAlarm;	
	if(signal(14,handler) == SIG_ERR){
			ERR_MSG("signal");
	}


	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0){
		ERR_MSG("socket");
	}
	SUC_MSG("socket");

	struct sockaddr_in cin;
	cin.sin_family      = AF_INET;
	cin.sin_port        = htons(PORT);
	cin.sin_addr.s_addr = inet_addr(IP);

	if(bind(sfd,(struct sockaddr*)&cin,sizeof(struct sockaddr_in)) < 0){
		ERR_MSG("bind");
	}
	SUC_MSG("bind");

	struct sockaddr_in sin;
	sin.sin_family      = AF_INET;
	sin.sin_port        = htons(SERVERPORT);
	sin.sin_addr.s_addr = inet_addr(SERVERIP);

	if(connect(sfd,(struct sockaddr*)&sin,sizeof(struct sockaddr_in)) < 0){
		ERR_MSG("connect");
	}
	SUC_MSG("connect");
	
	alarm(5);
	char buf[128];

	while(1){
		bzero(buf,sizeof(buf));
		printf("Enter something to send:");
		scanf("%s",buf);
		int res = send(sfd,buf,sizeof(buf),0);
		if(res < 0){
			ERR_MSG("send");
		}
		bzero(buf,sizeof(buf));
		res = recv(sfd,buf,sizeof(buf),0);
		printf("%s\n",buf);
		if(myAlarm == 14){
			break;
		}
	}

	if(close(sfd) < 0){
		ERR_MSG("close");
	}

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值