嵌入式学习

笔记

作业 

        1. TCP通信模型。

        tcp_ser.c文件

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

#define SER_PORT 8888
#define SER_IP "192.168.125.176"

int main(int argc, const char *argv[])
{
	//1. 为通信创建一个端点
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sfd)
	{
		perror("scoket error");
		return -1;
	}
	printf("socket success sfd=%d\n",sfd);

	//2. 绑定ip和端口号
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(SER_PORT);
	sin.sin_addr.s_addr=inet_addr(SER_IP);

	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
	{
		perror("bind");
		return -1;
	}
	printf("bind success\n");
	
	//3. 将套接字设置成被动监听状态
	if(listen(sfd,128) == -1)
	{
		perror("listen");
		return -1;
	}
	printf("listen success\n");

	//4. 阻塞等待客户端的链接
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);

	int newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
	if(-1 == newfd)
	{
		perror("accept");
		return -1;
	}
	printf("accept success\n");
	printf("[%s:%d]:发来连接请求\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
	
	//5. 与客户端进行相互通信
	char rbuf[128]="";

	while(1)
	{
		bzero(rbuf,sizeof(bzero));
		int ret=recv(newfd,rbuf,sizeof(rbuf),0);
		if(0 == ret)
		{
			printf("客户端已经下线\n");
			break;
		}
		printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);

		strcat(rbuf,"*_*");

		send(newfd,rbuf,strlen(rbuf),0);
		printf("发送成功\n");
	}

	//6. 关闭套接字
	close(newfd);
	close(sfd);

	return 0;
}

        tcp_cli.c文件

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

#define SER_PORT 8888
#define SER_IP "192.168.125.176"
#define CLI_PORT 6666
#define CLI_IP "192.168.125.176"

int main(int argc, const char *argv[])
{
	//1. 创建用于通信的套接字文件描述符
	int cfd=socket(AF_INET,SOCK_STREAM,0);
	if(-1 == cfd)
	{
		perror("socket error");
		return -1;
	}
	printf("cfd=%d\n",cfd);

	//2. 绑定的IP地址和端口号
	struct sockaddr_in cin;
	cin.sin_family=AF_INET;
	cin.sin_port=htons(CLI_PORT);
	cin.sin_addr.s_addr=inet_addr(CLI_IP);

	if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) == -1)
	{
		perror("bind");
		return -1;
	}
	printf("bind success\n");

	//3. 连接服务器
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(SER_PORT);
	sin.sin_addr.s_addr=inet_addr(SER_IP);

	if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
	{
		perror("connect error");
		return -1;
	}
	printf("connect success\n");

	//4. 数据收发
	char wbuf[128]="";
	char rbuf[128]="";
	while(1)
	{
		fgets(wbuf,sizeof(wbuf),stdin);
		wbuf[strlen(wbuf)-1]=0;
		send(cfd,wbuf,strlen(wbuf),0);
		printf("发送成功\n");
		bzero(rbuf,sizeof(rbuf));
		recv(cfd,rbuf,sizeof(rbuf),0);
		printf("服务器发来的消息为:%s\n",rbuf);
	}

	//5. 关闭连接
	close(cfd);

	return 0;
}

        2. UDP通信模型。 

         udp_ser.c文件

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

#define SER_PORT 8888
#define SER_IP "192.168.125.176"

int main(int argc, const char *argv[])
{
	//1. 创建用于通信的套接字文件描述符
	int sfd=socket(AF_INET,SOCK_DGRAM,0);
	if(-1 == sfd)
	{
		perror("socket error");
		return -1;
	}
	printf("socket sucess\n");
	printf("sfd=%d\n",sfd);

	//2. 绑定IP地址和端口号
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(SER_PORT);
	sin.sin_addr.s_addr=inet_addr(SER_IP);
	
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success\n");

	//3. 数据收发
	char rbuf[128]="";
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);

	while(1)
	{
		bzero(rbuf,sizeof(rbuf));
		recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,&addrlen);
		printf("收到消息为:%s\n",rbuf);
		strcat(rbuf,"*_*");
		if(sendto(sfd,rbuf,strlen(rbuf),0,(struct sockaddr*)&cin,sizeof(cin)) == -1)
		{
			perror("sendto error");
			return -1;
		}
		printf("发送成功\n");
	}

	//4.关闭套接字
	close(sfd);

	return 0;
}

        udp_cli.c文件

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

#define SER_PORT 8888
#define SER_IP "192.168.125.176"
#define CLI_PORT 6666
#define CLI_IP "192.168.125.176"
 
int main(int argc, const char *argv[])
{
	//1、创建用于通信的套接字文件描述符
	int cfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == cfd)
	{
		perror("socket error");
		return -1;
	}
	printf("cfd = %d\n", cfd);           
 
	/*2. 绑定IP地址和端口号
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(SER_PORT);
	sin.sin_addr.s_addr = inet_addr(SER_IP);
 
	if(bind(cfd, (struct sockaddr*)&sin, sizeof(sin)) ==-1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success\n");
	*/
	//3. 数据收发
	char wbuf[128] = "";
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(SER_PORT);
	sin.sin_addr.s_addr = inet_addr(SER_IP);
	char rbuf[128] = "";
	while(1)
	{
		bzero(wbuf,sizeof(wbuf));
		bzero(rbuf, sizeof(rbuf));
		fgets(wbuf, sizeof(wbuf), stdin);
		wbuf[strlen(wbuf)-1] = 0;
		sendto(cfd, wbuf, strlen(wbuf), 0, (struct sockaddr*)&sin, sizeof(sin));
		printf("发送成功\n");
		recvfrom(cfd, rbuf, sizeof(rbuf), 0,NULL, NULL);
		printf("收到服务器消息为:%s\n", rbuf);
	}
	
	//4、关闭套接字
	close(cfd);

	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值