码迷,mamicode.com
首页 > 编程语言 > 详细

c语言的tcp和udp客户端和服务器

时间:2016-06-21 17:07:10      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

都是最简单的用来记忆。

this is my 的git地址:https://github.com/yanjinyun/cLanguageTcpUdp

tcp最简单的服务器:

int main(int argc, const char *argv[])
{
    int listenfd, acceptfd;
    struct sockaddr_in sin, cin;
    socklen_t clen;
    char buf[1024];

    signal(SIGCHLD, SIG_IGN);

    listenfd = socket(PF_INET, SOCK_STREAM, 0);

    sin.sin_family = PF_INET;
    sin.sin_port = htons(atoi(argv[2]));
    sin.sin_addr.s_addr = inet_addr(argv[1]);
    bind(listenfd, (struct sockaddr *)&sin, sizeof(sin));

    listen(listenfd, 5);

    while(1)
    {
        clen = sizeof(cin);
        printf("listen ......\n");
        acceptfd = accept(listenfd, (struct sockaddr *)&cin, &clen);
        printf("client %s %d connect\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));

        if(fork() == 0)
        {
            close(listenfd);

            while(1)
            {
                bzero(buf, sizeof(buf));
                if(recv(acceptfd, buf, sizeof(buf), 0) == 0)
                {
                    printf("client %s %d exit\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));
                    exit(0);
                }
                printf("recv(%s %d): %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
            }
        }

        close(acceptfd);
    }

    return 0;
}

tcp最简单的客户端:

int main(int argc, const char *argv[])
{
    int cfd = socket(PF_INET, SOCK_STREAM, 0);
    struct sockaddr_in cin;
    char buf[1024];

    cin.sin_family = PF_INET;
    cin.sin_port = htons(atoi(argv[2]));
    cin.sin_addr.s_addr = inet_addr(argv[1]);
    connect(cfd, (struct sockaddr *)&cin, sizeof(cin));

    printf("connect ip(%s) port(%s) server ok\n", argv[1], argv[2]);

    if(fork() == 0)
    {
        while(1)
        {
            bzero(buf, sizeof(buf));
            recv(cfd, buf, sizeof(buf), 0);
            printf("recv: %s\n", buf);
        }
    }

    while(1)
    {
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = \0;
        send(cfd, buf, strlen(buf)+1, 0);
    }

    return 0;
}

udp的最简单的服务器:

int main(int argc, const char *argv[])
{
	int sfd;
	struct sockaddr_in sin, cin;
	char buf1[1024], buf2[1024], buf3[1024];
	socklen_t clen;

	sfd = socket(PF_INET, SOCK_DGRAM, 0);

	sin.sin_family = PF_INET;
	sin.sin_port = htons(atoi(argv[2]));
	sin.sin_addr.s_addr = inet_addr(argv[1]);
	bind(sfd, (struct sockaddr *)&sin, sizeof(sin));

	if(fork() == 0)
	{
		while(1)
		{
			clen = sizeof(cin);
			bzero(buf1, sizeof(buf1));
			recvfrom(sfd, buf1, sizeof(buf1), 0, (struct sockaddr *)&cin, &clen);
			printf("recvfrom: %s %d %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf1);
		}
	}

	while(1)
	{
		fscanf(stdin, "%s%s%s", buf1, buf2, buf3);
		cin.sin_family = PF_INET;
		cin.sin_port = htons(atoi(buf2));
		cin.sin_addr.s_addr = inet_addr(buf1);
		sendto(sfd, buf3, strlen(buf3)+1, 0, (struct sockaddr *)&cin, sizeof(cin));
	}

	return 0;
}

udp最简单的客户端:

int main(int argc, const char *argv[])
{
	int sfd;
	struct sockaddr_in cin;
	char buf1[1024], buf2[1024], buf3[1024];
	socklen_t clen;

	sfd = socket(PF_INET, SOCK_DGRAM, 0);

	if(fork() == 0)
	{
		while(1)
		{
			clen = sizeof(cin);
			bzero(buf1, sizeof(buf1));
			recvfrom(sfd, buf1, sizeof(buf1), 0, (struct sockaddr *)&cin, &clen);
			printf("recvfrom: %s %d %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf1);
		}
	}

	while(1)
	{
		fscanf(stdin, "%s%s%s", buf1, buf2, buf3);
		cin.sin_family = PF_INET;
		cin.sin_port = htons(atoi(buf2));
		cin.sin_addr.s_addr = inet_addr(buf1);
		sendto(sfd, buf3, strlen(buf3)+1, 0, (struct sockaddr *)&cin, sizeof(cin));
	}

	return 0;
}

ftp最简单的服务器器:

#define N 256

void list(int connectfd)
{
	DIR *dir;
	struct dirent *d;
	char buf[N];

	if((dir=opendir((const char *)get_current_dir_name())) == NULL)
	{
		perror("opendir");
		exit(1);
	}
	while((d=readdir(dir)) != NULL)
	{
		if(d->d_name[0] == ‘.‘)
		{
			continue;
		}
		strcpy(buf, d->d_name);
		send(connectfd, buf, N, 0);
	}

	closedir(dir);
}

void get(int connectfd, char *filename)
{
	char buf[N];
	FILE *fp;
	int n;

	if((fp=fopen(filename, "r")) == NULL)
	{
		strcpy(buf, "no");
		send(connectfd, buf, sizeof(buf), 0);
	}

	strcpy(buf, "yes");
	send(connectfd, buf, sizeof(buf), 0);

	while((n=fread(buf, 1, sizeof(buf), fp)) > 0)
	{
		send(connectfd, buf, n, 0);
	}

	fclose(fp);
}

void put(int connectfd, char *filename)
{
	char buf[N];
	FILE *fp;
	int n;

	if((fp=fopen(filename, "r")) == NULL)
	{
		strcpy(buf, "no");
		send(connectfd, buf, sizeof(buf), 0);
		fp = fopen(filename, "w");
		while((n=recv(connectfd, buf, sizeof(buf), 0)) > 0)
		{
			fwrite(buf, 1, n, fp);
		}
		fclose(fp);

		return;
	}

	strcpy(buf, "yes");
	send(connectfd, buf, sizeof(buf), 0);
}

int main(int argc, const char *argv[])
{
	FILE *fp;
	int n, listenfd, connectfd, i;
	struct sockaddr_in s, c;
	struct servent *sport;
	socklen_t clen;
	char *linep = NULL, buf[N], *arg[32];

	signal(SIGCHLD, SIG_IGN);

	if((fp=fopen("/etc/ftp.conf", "r")) == NULL)
	{
		perror("fopen");
		exit(1);
	}

	if(getline(&linep, &n, fp) == -1)
	{
		perror("getline");
		exit(0);
	}
	linep[strlen(linep)-1] = ‘\0‘;
	chdir(linep);

	if((sport=getservbyname("myftp", "tcp")) == NULL)
	{
		perror("getservbyname");
		exit(1);
	}

#ifdef DEBUG
	printf("%d %s\n", n, linep);
#endif

	if((listenfd=socket(PF_INET, SOCK_STREAM, 0)) == -1)
	{
		perror("socket");
		exit(1);
	}
	
	s.sin_family = PF_INET;
	s.sin_port = sport->s_port; 
	s.sin_addr.s_addr = htonl(INADDR_ANY);
	if(bind(listenfd, (struct sockaddr *)&s, sizeof(s)) == -1)
	{
		perror("bind");
		exit(1);
	}

	listen(listenfd, 5);

	while(1)
	{
		clen = sizeof(c);
		if((connectfd=accept(listenfd, (struct sockaddr *)&c, &clen)) == -1)
		{
			perror("accept");
			exit(1);
		}

		if(fork() == 0)
		{
			close(listenfd);

			recv(connectfd, buf, sizeof(buf), 0);

			arg[0] = strtok(buf, " ");
			for(i=1; arg[i-1]!=NULL; i++)
			{
				arg[i]=strtok(NULL, " ");
			}

			if(strcmp(arg[0], "list") == 0)
			{
				list(connectfd);
			}

			if(strcmp(arg[0], "get") == 0)
			{
				get(connectfd, arg[1]);
			}

			if(strcmp(arg[0], "put") == 0)
			{
				put(connectfd, arg[1]);
			}

			close(connectfd);
			exit(0);
		}

		close(connectfd);
	}
}

  

  

ftp最简单的客户端器:

#define N 256

void list(int connectfd)
{
	DIR *dir;
	struct dirent *d;
	char buf[N];

	if((dir=opendir((const char *)get_current_dir_name())) == NULL)
	{
		perror("opendir");
		exit(1);
	}
	while((d=readdir(dir)) != NULL)
	{
		if(d->d_name[0] == ‘.‘)
		{
			continue;
		}
		strcpy(buf, d->d_name);
		send(connectfd, buf, N, 0);
	}

	closedir(dir);
}

void get(int connectfd, char *filename)
{
	char buf[N];
	FILE *fp;
	int n;

	if((fp=fopen(filename, "r")) == NULL)
	{
		strcpy(buf, "no");
		send(connectfd, buf, sizeof(buf), 0);
	}

	strcpy(buf, "yes");
	send(connectfd, buf, sizeof(buf), 0);

	while((n=fread(buf, 1, sizeof(buf), fp)) > 0)
	{
		send(connectfd, buf, n, 0);
	}

	fclose(fp);
}

void put(int connectfd, char *filename)
{
	char buf[N];
	FILE *fp;
	int n;

	if((fp=fopen(filename, "r")) == NULL)
	{
		strcpy(buf, "no");
		send(connectfd, buf, sizeof(buf), 0);
		fp = fopen(filename, "w");
		while((n=recv(connectfd, buf, sizeof(buf), 0)) > 0)
		{
			fwrite(buf, 1, n, fp);
		}
		fclose(fp);

		return;
	}

	strcpy(buf, "yes");
	send(connectfd, buf, sizeof(buf), 0);
}

int main(int argc, const char *argv[])
{
	FILE *fp;
	int n, listenfd, connectfd, i;
	struct sockaddr_in s, c;
	struct servent *sport;
	socklen_t clen;
	char *linep = NULL, buf[N], *arg[32];

	signal(SIGCHLD, SIG_IGN);

	if((fp=fopen("/etc/ftp.conf", "r")) == NULL)
	{
		perror("fopen");
		exit(1);
	}

	if(getline(&linep, &n, fp) == -1)
	{
		perror("getline");
		exit(0);
	}
	linep[strlen(linep)-1] = ‘\0‘;
	chdir(linep);

	if((sport=getservbyname("myftp", "tcp")) == NULL)
	{
		perror("getservbyname");
		exit(1);
	}

#ifdef DEBUG
	printf("%d %s\n", n, linep);
#endif

	if((listenfd=socket(PF_INET, SOCK_STREAM, 0)) == -1)
	{
		perror("socket");
		exit(1);
	}
	
	s.sin_family = PF_INET;
	s.sin_port = sport->s_port; 
	s.sin_addr.s_addr = htonl(INADDR_ANY);
	if(bind(listenfd, (struct sockaddr *)&s, sizeof(s)) == -1)
	{
		perror("bind");
		exit(1);
	}

	listen(listenfd, 5);

	while(1)
	{
		clen = sizeof(c);
		if((connectfd=accept(listenfd, (struct sockaddr *)&c, &clen)) == -1)
		{
			perror("accept");
			exit(1);
		}

		if(fork() == 0)
		{
			close(listenfd);

			recv(connectfd, buf, sizeof(buf), 0);

			arg[0] = strtok(buf, " ");
			for(i=1; arg[i-1]!=NULL; i++)
			{
				arg[i]=strtok(NULL, " ");
			}

			if(strcmp(arg[0], "list") == 0)
			{
				list(connectfd);
			}

			if(strcmp(arg[0], "get") == 0)
			{
				get(connectfd, arg[1]);
			}

			if(strcmp(arg[0], "put") == 0)
			{
				put(connectfd, arg[1]);
			}

			close(connectfd);
			exit(0);
		}

		close(connectfd);
	}
}

 

c语言的tcp和udp客户端和服务器

标签:

原文地址:http://www.cnblogs.com/coding4/p/5603889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!