嵌入式学习Day28

TCP服务器

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) {\
fprintf(stderr,"line: %d\n",__LINE__);\
perror(msg);\
}
#define IP "192.168.8.66"//本机IP地址
#define PORT 9999        //1024-49151
int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("sfd=%d\n",sfd);

	//允许端口快速重用
	int reuse=1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速重用成功\n");

	//填充地址信息结构体
	//真实的地址信息结构体根据地址族指定 AF_NET: man 7 IP
	struct sockaddr_in sin;
	sin.sin_family =AF_INET;//必须填AF_NET
	sin.sin_port   =htons(PORT);//端口号
	sin.sin_addr.s_addr =inet_addr(IP);//本机IP

    //将本机IP和端口绑定到套接字
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success __%d__\n",__LINE__);

	//将套接字设置为被监听状态,监听是否有客户端连接成功
	if(listen(sfd,128)<0)
	{
		ERR_MSG("lisen");
		return -1;
	}
	printf("lisen success  __%d__\n",__LINE__);

	struct sockaddr_in cin; //存储连接成功的客户端信息

	socklen_t addrlen=sizeof(cin);

	//阻塞函数,从已完成连接的队列头获取一个客户端信息,生成一个新文件符
	//改文件描述符才是与客户端通信的文件描述符
	int newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
	if(newfd<0)
	{
		ERR_MSG("acceot");
		return -1;
	}
	printf("[%s:%d] newfd=%d 连接成功__%d__\n",\
			inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);


	

	char buf[128]="";
	ssize_t res=0;
	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 客户端下线__%d__\n",\
					inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
			break;
		}
		printf("[%s:%d] newfd=%d :%s__%d__\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);
		//发送 将数据拼接字符发送回去
		strcat(buf,"*_*");
		if(send(newfd,buf,sizeof(buf),0)<0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("发送成功\n");

	}
	close(newfd);
	close(sfd);

	
	
	return 0;
}

TCP客户端

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

#define ERR_MSG(msg) do{\
    fprintf(stderr, "line:__%d__ ", __LINE__);\
    perror(msg);\
}while(0)

#define SER_PORT 8889           //服务器的端口
#define SER_IP  "192.168.9.46"  //服务器绑定的IP

#define CLI_PORT 8889          //客户端需要绑定的端口号
#define CLI_IP "192.168.8.66"   //本机IP,ifconfig

int main(int argc, const char *argv[])
{
    //创建流式套接字
    int cfd = socket(AF_INET, SOCK_STREAM, 0);
    if(cfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
	//允许端口快速重用
	int reuse=1;
	if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速重用成功\n");

    //绑定客户端自身的地址信息结构体--->非必须绑定,可以选择不绑
    //如果选择不绑定,则操作系统会默认绑定一个端口号给这个客户端

  
    /****************************************************/
    //填充客户端自身的IP和端口
    struct sockaddr_in cin;
    cin.sin_family       = AF_INET;
    cin.sin_port         = htons(CLI_PORT);     //客户端的port
    cin.sin_addr.s_addr  = inet_addr(CLI_IP);   //本机IP,ifconfig

    if(bind(cfd, (struct sockaddr*)&cin, sizeof(cin)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("client bind success __%d__\n",__LINE__);
    /**********************************************************/


    //填充要连接的服务器的IP和端口 ,根据地址族指定
    struct sockaddr_in sin;
    sin.sin_family      = AF_INET;              //必须填AF_INET;
    sin.sin_port        = htons(SER_PORT);      //服务器绑定的端口号
    sin.sin_addr.s_addr = inet_addr(SER_IP);    //服务器绑定的IP地址

    //连接服务器
    if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
    {
        ERR_MSG("connect");
        return -1;
    }
    printf("connect server success __%d__\n",__LINE__);

    char buf[128] = "";                                                            
    ssize_t res = 0;
    while(1)
    {
        bzero(buf, sizeof(buf));
        //发送数据给服务器,服务器收多少,发送多少字节
        printf("请输入>>>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = 0;

        if(send(cfd, buf, sizeof(buf), 0) < 0)
        {
            ERR_MSG("send");
            return -1;
        }
        printf("发送成功\n");

        //接收服务器返回的数据
        bzero(buf, sizeof(buf));
        res = recv(cfd, buf, sizeof(buf), 0);
        if(res < 0)
        {
            ERR_MSG("recv");
            return -1;
        }
        else if(0 == res)
        {
            printf("cfd=%d 服务器端下线__%d__\n",cfd,__LINE__);
            break;
        }

        printf("cfd=%d:%s __%d__\n",cfd, buf,__LINE__);
    }

    //关闭文件描述符
    close(cfd);
    return 0;
}

UDP服务器

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

#define ERR_MSG(msg) do{\
    fprintf(stderr, "line:%d ", __LINE__);\
    perror(msg);\
}while(0)

#define PORT 6767               //1024~49151
#define IP  "192.168.8.66"    //IP地址,本机IP ifconfig
int main(int argc, const char *argv[])
{
    //创建报式套接字
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("socket create success  sfd=%d __%d__\n", sfd, __LINE__);


    //允许端口快速被复用


    //填充服务器自身的地址信息结构体 AF_INET : man 7 IP
    struct sockaddr_in sin;
    sin.sin_family      = AF_INET;  //必须填AF_INET;
    sin.sin_port        = htons(PORT);  //服务器绑定的端口,网络字节序
    sin.sin_addr.s_addr = inet_addr(IP);    //服务器绑定的IP,本机IP ifconfig

    //绑定--->必须绑定
    if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("bind success __%d__\n", __LINE__);
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);

	char buf[128]="";
	ssize_t res=0;
	while(1)
	{
		bzero(buf,sizeof(buf));
		//接受数据,必须接受数据包的发送方地址信息,给sendto使用
		res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
		if(res<0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
         printf("[%s : %d] : %s\n", \
                inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);


        //发送数据  谁发给我,我发给谁
        strcat(buf, "*_*");
        if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, sizeof(cin)) < 0)
        //if(send(sfd, buf, sizeof(buf), 0) < 0)    //错误的,不知道该发给谁
        {
            ERR_MSG("sendto");
            return -1;
        }
        printf("发送成功\n");
    }

    //关闭所有文件描述符
    close(sfd);
    return 0;
}

UDP客户端

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

#define ERR_MSG(msg) do{\
    fprintf(stderr, "line:%d ", __LINE__);\
    perror(msg);\
}while(0)
                                                                                                                                                  
#define SER_PORT 6767               //1024~49151
#define SER_IP  "192.168.8.66"    //IP地址,本机IP ifconfig

#define CLI_PORT 8888
#define CLI_IP  "192.168.8.66"

int main(int argc, const char *argv[])
{
    //创建报式套接字
    int cfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(cfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("socket create success  cfd=%d __%d__\n", cfd, __LINE__);
    //*******************************************
    //绑定  非必须绑定
    //如果不绑定,操作系统会自动绑定本机IP,从49152~65535范围内随机一个未被使用的端口号

    //填充客户端自身的地址信息结构体, AF_INET : man 7 ip
    struct sockaddr_in cin;
    cin.sin_family      = AF_INET;          
    cin.sin_port        = htons(CLI_PORT);  //客户端自身的端口号的网络字节序 不能与服务器一致1024~49151
    cin.sin_addr.s_addr = inet_addr(CLI_IP);    //客户端的本机IP

    if(bind(cfd, (struct sockaddr*)&cin, sizeof(cin)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("client bind success __%d__\n", __LINE__);
    //*********************************************

    //填充服务器的地址信息结构体,供给下面的sendto使用
    struct sockaddr_in sin;
    sin.sin_family      = AF_INET;  //必须填AF_INET;
    sin.sin_port        = htons(SER_PORT);  //服务器绑定的端口,网络字节序
    sin.sin_addr.s_addr = inet_addr(SER_IP);    //服务器IP地址

    char buf[128] = "";
    ssize_t res = 0;

    struct sockaddr_in rcvaddr;     //存储获取到的数据包是从谁那里来的
    socklen_t addrlen = sizeof(rcvaddr);

    while(1)
    {
        bzero(buf, sizeof(buf));
        //发送数据给服务器
		printf("请输入>>>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = 0;

        //发送给服务器,所以上面的代码,需要填充好服务器的地址信息结构
        //想要发送给谁,就填谁的地址信息结构体
        if(sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
        {
            ERR_MSG("sendto");
            return -1;
        }
        printf("发送成功\n");

        //接收数据
        res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&rcvaddr, &addrlen);
        if(res < 0)
        {
            ERR_MSG("recvfrom");
            return -1;
        }
        printf("[%s : %d] : %s\n", \
                inet_ntoa(rcvaddr.sin_addr), ntohs(rcvaddr.sin_port), buf);


    }

    //关闭所有文件描述符
    close(cfd);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值