网络编程第二天

TCP服务器:

#include 

#define IP "192.168.170.131"
#define PORT 8888

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 __%d__\n", sfd, __LINE__);

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

	//允许端口快速被复用---监测端口号是否真的有进程在占用,如果没有,则快速复用
	int reuse = 1;
	if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速复用成功\n");

	//绑定服务器IP和端口
	if (bind(sfd, (struct sockaddr *)&sin, (socklen_t)sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");

	//设置监听
	if (listen(sfd, 128) < 0)
	{
		ERR_MSG("listen");
		return -1;
	}

	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	//连接成功
	int newfd = accept(sfd, (struct sockaddr *)&cin, &addrlen);
	if (newfd < 0)
	{
		ERR_MSG("accept");
		return -1;
	}
	printf("[%s : %d] 连接成功\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));

	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 (res = 0)
		{
			printf("客户端断开连接\n");
			return 0;
		}
		printf("[%s : %d] : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);

		//发送数据
		bzero(buf, sizeof(buf));
		printf("请输入>>>");
		fgets(buf, sizeof(buf), stdin);
		buf[strlen(buf) - 1] = '\0';

		if (send(newfd, buf, strlen(buf), 0) < 0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("send success\n");
	}

	close(newfd);
	close(sfd);

	return 0;
}

TCP客户端:

#include 

#define IP "192.168.170.131"
#define PORT 8888

int main(int argc, const char *argv[])
{
	//创建套接字
	int cfd = socket(AF_INET, SOCK_STREAM, 0);
	if (cfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("sfd = %d __%d__\n", cfd, __LINE__);

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

	//连接服务器
	if (connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
	{
		ERR_MSG("connect");
		return -1;
	}
	printf("服务器连接成功\n");

	ssize_t res = 0;
	char buf[128] = "";
	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("send success\n");

		bzero(buf, sizeof(buf));
		//接收数据
		res = recv(cfd, buf, sizeof(buf), 0);
		if (res < 0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if (res = 0)
		{
			printf("服务器断开连接\n");
			return 0;
		}
		printf("[%s : %d] : %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), buf);
	}

	close(cfd);

	return 0;
}

UDP服务器:

#include 

#define IP "192.168.170.131"
#define PORT 8888

int main(int argc, const char *argv[])
{
    //创建套接字
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("sfd = %d __%d__\n", sfd, __LINE__);

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

    //允许端口快速被复用---监测端口号是否真的有进程在占用,如果没有,则快速复用
    int reuse = 1;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("允许端口快速复用成功\n");

    //绑定服务器IP和端口
    if (bind(sfd, (struct sockaddr *)&sin, (socklen_t)sizeof(sin)) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("bind success\n");

    struct sockaddr_in cin;
    socklen_t addrlen = sizeof(cin);

    ssize_t res = 0;
    char buf[128] = "";
    while (1)
    {
        bzero(buf, sizeof(buf));
        //接收数据
        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);

        //发送数据
        bzero(buf, sizeof(buf));
        printf("请输入>>>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';

        if (sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&cin, addrlen) < 0)
        {
            ERR_MSG("sendto");
            return -1;
        }
        printf("send success\n");
    }

    close(sfd);

    return 0;
}

UDP客户端:

#include 

#define IP "192.168.170.131"
#define PORT 8888

int main(int argc, const char *argv[])
{
    //创建套接字
    int cfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (cfd < 0)
    {
        ERR_MSG("socket");
        return -1;
    }
    printf("cfd = %d __%d__\n", cfd, __LINE__);

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

    struct sockaddr_in rcvaddr;
    socklen_t addrlen = sizeof(rcvaddr);

    ssize_t res = 0;
    char buf[128] = "";
    while (1)
    {
        //发送数据
        bzero(buf, sizeof(buf));
        printf("请输入>>>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';

        if (sendto(cfd, buf, strlen(buf), 0, (struct sockaddr *)&rcvaddr, addrlen) < 0)
        {
            ERR_MSG("sendto");
            return -1;
        }
        printf("send success\n");

        bzero(buf, sizeof(buf));
        //接收数据
        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;
}

你可能感兴趣的:(网络编程,网络,c语言,linux)