linux下c语言实现TCP网络编程对话模拟socket

程序基本功能:

编写使用TCP协议的服务器程序和客户端程序;两者建立连接实现模拟对话的功能,即:客户端向服务器发送字符串,服务器打印收到的字符串,并向客户端发送应答。

服务器端:

//server
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define BUFSIZE 1024
#define PORT 8000
void main(void){
	int client_sockfd, my_sockfd;
	struct sockaddr_in my_addr;
	struct sockaddr_in client_addr;
	int len;
	int sin_size;
	char buf[BUFSIZE];
	memset(&my_addr, 0, sizeof(my_addr));//清空
	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(PORT);
	my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	//设置协议
	if((my_sockfd = socket(PF_INET, SOCK_STREAM, 0))==-1){
		printf("socket error\n");
		exit(1);
	} 
	//绑定地址
	if(bind(my_sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr))==-1){
		printf("bind error\n");
		exit(1);
	}
	//设定最大连接数
	if(listen(my_sockfd, 5)==-1){
		printf("listen error\n");
		exit(1);
	}
	sin_size = sizeof(struct sockaddr_in);
	//等待客户端连接,从listen队列中取出连接
	if((client_sockfd = accept(my_sockfd, (struct sockaddr*)&my_addr, &sin_size))==-1){
		printf("accept error\n");
		exit(1);
	}
	printf("client connected\n");
	len = send(client_sockfd,"welcome!\n", 9, 0);
	while((len = recv(client_sockfd, buf, BUFSIZE, 0))>0){
		buf[len] = '\0';
		printf("message from client:\n%s\nEnter your anwser:\n", buf);
		scanf("%s", buf);
		if(send(client_sockfd, buf, strlen(buf), 0) < 0){
	printf("send error\n");
	exit(1);
	}
	}
	close(my_sockfd);
	close(client_sockfd);
}

客户端:

//client
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define BUFSIZE 1024
#define PORT 8000
void main(void){
	int len;
	char buf[BUFSIZE];
	struct sockaddr_in clientaddr;
	int sockfd;
	memset(&clientaddr, 0, sizeof(clientaddr));
	clientaddr.sin_family = AF_INET;
	clientaddr.sin_port = htons(PORT);
	clientaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	if((sockfd = socket(PF_INET, SOCK_STREAM, 0))==-1){
		printf("socket error\n");
		exit(1);
	}
	if((connect(sockfd, (struct sockaddr*)&clientaddr, sizeof(struct sockaddr))) == -1){
		printf("connect error\n");
		exit(1);
	}
	printf("connected\n");
	len = recv(sockfd, buf, BUFSIZE, 0);
	buf[len] = '\0';
	printf("Message from server:\n%s\nEnter your anwser here:\n", buf);
	scanf("%s", buf);
	while(1){
		if(strcmp(buf, "quit")== 0)
	break;
		if(send(sockfd, buf, strlen(buf), 0) < 0){
			printf("send error\n");
			exit(1);
		}
		len = recv(sockfd, buf, BUFSIZE, 0);
		buf[len] = '\0';
		printf("Message from server:\n%s\nEnter your anwser here:\n", buf);
		scanf("%s", buf);
	}
	close(sockfd);
}

参考博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值