网络编程第二天

tcp
  1	#include "../head.h"
     2	#define IP "192.168.124.80"
     3	#define PORT 6666
     4	int main(int argc, const char *argv[])
     5	{
     6		int sfd = socket(AF_INET,SOCK_STREAM,0);
     7		if(sfd < 0)
     8		{
     9			ERR_MSG("socket");
    10			return -1;
    11		}
    12		printf("socket create success sfd=%d __%d__\n",sfd,__LINE__);
    13		struct sockaddr_in sin;
    14		sin.sin_family 		=AF_INET;
    15		sin.sin_port 		= htons(PORT);
    16		sin.sin_addr.s_addr = inet_addr(IP);
    17		if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    18		{
    19			ERR_MSG("bind");
    20			return -1;
    21		}
    22		printf("bind success __%d__\n",__LINE__);
    23		if(listen(sfd,128) < 0)
    24		{
    25			ERR_MSG("lsiten");
    26			return -1;
    27		}
    28		printf(" listen success __%d__\n",__LINE__);
    29		struct sockaddr_in cin;
    30		socklen_t addrlen = sizeof(cin);
    31		int newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
    32		if(newfd < 0)
    33		{
    34			ERR_MSG("accept");
    35			return -1;
    36		}
    37		printf("newfd=%d 客户端连接成功 __%d__\n",newfd,__LINE__);
    38		printf("%s : %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
    39		char buf[128] = "";
    40		ssize_t res = 0;
    41		while(1)
    42		{
    43			bzero(buf,sizeof(buf));
    44			res = recv(newfd,buf,sizeof(buf),0);
    45			if(res < 0)
    46			{
    47				ERR_MSG("recv");
    48				return -1;
    49			}
    50			else if(0 == res)
    51			{
    52				printf("newfd=%d 客户已下线 __%d__\n",newfd,__LINE__);
    53				printf("%s : %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
    54				break;
    55			}
    56			printf("%s : %d 说:",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
    57			printf("%s\n",buf);
    58			LINE;
    59			printf("请回复上帝>>>>");
    60			scanf("%s",buf);
    61			if(send(newfd,buf,sizeof(buf),0) < 0)
    62			{
    63				ERR_MSG("send");
    64				return -1;
    65			}
    66			printf("send success");
    67			LINE;
    68		}
    69		close(newfd);
    70		if(close(sfd) < 0)
    71		{
    72			ERR_MSG("close");
    73			return -1;
    74		}
    75	
    76		return 0;
    77	}
    78	#include "../head.h"
    79	#define IP "192.168.124.76"
    80	#define PORT 5555
    81	int main(int argc, const char *argv[])
    82	{
    83		int cfd = socket(AF_INET,SOCK_STREAM,0);
    84		if(cfd < 0)
    85		{
    86			ERR_MSG("socket");
    87			return -1;
    88		}
    89		printf("socket create success cfd=%d ",cfd);
    90		LINE;
    91		struct sockaddr_in sin;
    92		sin.sin_family 		=AF_INET;
    93		sin.sin_port 		= htons(PORT);
    94		sin.sin_addr.s_addr = inet_addr(IP);
    95		if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
    96		{
    97			ERR_MSG("connect");
    98			return -1;
    99		}
   100		printf("connect success ");
   101		LINE;
   102		char buf[128] = "";
   103		ssize_t res = 0;
   104		while(1)
   105		{
   106			printf("上帝您请说>>>>");
   107			fgets(buf,sizeof(buf),stdin);
   108			buf[strlen(buf)-1] = '\0';
   109			if(send(cfd,buf,sizeof(buf),0) < 0)
   110			{
   111				ERR_MSG("send");
   112				return -1;
   113			}
   114			printf("send success");
   115			LINE;
   116	
   117	
   118			bzero(buf,sizeof(buf));
   119			res = recv(cfd,buf,sizeof(buf),0);
   120			if(res < 0)
   121			{
   122				ERR_MSG("recv");
   123				return -1;
   124			}
   125			else if(0 == res)
   126			{
   127				printf("服务器已下线 ");
   128				LINE;
   129				printf("%s : %d\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));
   130				break;
   131			}
   132			printf("%s : %d 说:",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));
   133			printf("%s\n",buf);
   134			LINE;
   135		}
   136		if(close(cfd) < 0)
   137		{
   138			ERR_MSG("close");
   139			return -1;
   140		}
   141	
   142		return 0;
   143	}
udp
#include "../head.h"
#define IP "192.168.124.80"
#define PORT 6666
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__);
	struct sockaddr_in sin;
	sin.sin_family 		=AF_INET;
	sin.sin_port 		= htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
	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));
		res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);

		if(res < 0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
		printf("%s : %d 说:",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
		printf("%s\n",buf);
		LINE;
		printf("请回复上帝>>>>");
		fgets(buf,sizeof(buf),stdin);
		if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin)) < 0)
		{
			ERR_MSG("send");
			return -1;
		}
		printf("send success");
		LINE;
	}
	if(close(sfd) < 0)
	{
		ERR_MSG("close");
		return -1;
	}

	return 0;
}
#include "../head.h"
#define IP "192.168.124.80"
#define PORT 6666
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 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);

	struct sockaddr_in rin;
	socklen_t addrlen = sizeof(rin);
	char buf[128] = "";
	ssize_t res = 0;
	while(1)
	{	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("send success");
		LINE;

		bzero(buf,sizeof(buf));
		res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&rin,&addrlen);

		if(res < 0)
		{
			ERR_MSG("recvfrom");
			return -1;
		}
		printf("%s : %d 说:",inet_ntoa(rin.sin_addr),ntohs(rin.sin_port));
		printf("%s\n",buf);
		LINE;
	}
		if(close(cfd) < 0)
		{
			ERR_MSG("close");
			return -1;
		}

		return 0;
	}

你可能感兴趣的:(网络,php,开发语言)