SOCKET 编程TCP/IP、UDP

TCP/IP

资源:http://download.csdn.net/detail/mao0514/9061265

server:

#include<stdio.h>
#include<winsock2.h>

void main()
{
	SOCKET servsock,clisock;
	struct sockaddr_in sa;
	struct sockaddr_in cliaddr;
	int servport=6666;
	char buff[256];
	WSADATA ws;
	int len,err;
	// 初始化Winsock
	if(WSAStartup(0x0101,&ws)!=0)
	{
		printf("WSAStartup() failed!\n");
		return;
	}
	//创建套接字
	printf("Create Socket...\n");
	servsock=socket(AF_INET,SOCK_STREAM,0);
	//填充服务器地址结构
	memset(&sa,0,sizeof(sa));
	sa.sin_family=AF_INET;
	sa.sin_port=htons(servport);
	sa.sin_addr.s_addr=inet_addr("192.168.1.100");
	//sa.sin_addr.s_addr=inet_addr("127.0.0.1");
	
	//绑定套接字到服务器地址结构
	printf("Binding...\n");
	err=bind(servsock,(const sockaddr *)&sa,sizeof(sa));
	if(err!=0)
	{
		fprintf(stderr,"Bind failed:%d\n",WSAGetLastError());
		return;
	}
	
	//监听套接字
	printf("Listening...\n");
	err=listen(servsock,5);
	if(err!=0)
	{
		fprintf(stderr,"Listen failed:%d\n",WSAGetLastError());
		return;
	}
	
	//等待连接请求
	printf("Waitting Request...\n");
	len=sizeof(cliaddr);
	clisock=accept(servsock,(struct sockaddr *)&cliaddr,&len);

	len=recv(clisock,buff,sizeof(buff),0);
	if(len>0)
	{
		buff[len]=0;
	       printf("%s\n",buff);
	}
	
	printf("Accept Client:%s:%d\n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
	sprintf(buff,"Welcome you %s",inet_ntoa(cliaddr.sin_addr));
	
	//发送欢迎词
    send(clisock,buff,strlen(buff),0);
    Sleep(1000);
     
//	recv(clisock,buff,strlen(buff),0);
 //   printf("%s\n",buff);

	//关闭连接
    closesocket(clisock);
    closesocket(servsock);
    WSACleanup();
}
client:

#include<winsock2.h>
void main()
{
	SOCKET sock;
	struct sockaddr_in sa;
	int err;
	int servport=6666;
	char buff[256];
	int len;
	WSADATA ws;
	
	// 初始化Winsock
	if(WSAStartup(0x0101,&ws)!=0)
	{
		printf("WSAStartup() failed!\n");
		return;
	}
	//创建套接字
	sock=socket(AF_INET,SOCK_STREAM,0);
	
	//定义服务器地址结构
	memset(&sa,0,sizeof(sa));
	sa.sin_family=AF_INET;
	sa.sin_port=htons(servport);  
	sa.sin_addr.s_addr=inet_addr("192.168.1.100");
	
	// 连接服务器
	err=connect(sock,(const sockaddr*)&sa,sizeof(sa));
  //  printf("test1");
	while(1)
	{
	//	printf("test1");
		fgets(buff,256,stdin);
	//	for(int i=0;i<100;i++)
		send(sock,buff,strlen(buff),0);
		if(strncmp(buff,"exit",4)==0) break;
		len=recv(sock,buff,sizeof(buff),0);
		if(len>0)
		{
			buff[len]=0;
	       printf("%s\n",buff);
		}
	}

	//接收欢迎词
	memset(buff,0,sizeof(buff));
	len=recv(sock,buff,sizeof(buff),0);
	
	printf("%s\n",buff);
	//关闭连接
	closesocket(sock);
	WSACleanup();
}

UDP:

#include<stdlib.h>
#include<string.h>
#include<winsock2.h>

#define BUFSIZE 256

void main(void)
{
/////////////////////////初始化/////////////////////
	WSADATA wsaData;
	WSAStartup(0x0202,&wsaData);
	SOCKET m_socket;
	m_socket=socket(AF_INET,SOCK_DGRAM,0);
	if(INVALID_SOCKET==m_socket)
	{
		printf("套接字创建失败!");
		return;
	}
	SOCKADDR_IN addrSock;
	addrSock.sin_family=AF_INET;
	addrSock.sin_port=htons(6000);
	addrSock.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
	
	int retval;
	retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
	if(SOCKET_ERROR==retval)
	{
		closesocket(m_socket);
		printf("绑定失败!");
	}
/////////////////接收///////////////////////////
	SOCKADDR_IN addrFrom;
	int len=sizeof(SOCKADDR),len2;
	
	char recvBuf[200];
	char tempBuf[300];
	SOCKADDR_IN addrTo;
	addrTo.sin_family=AF_INET;
	addrTo.sin_port=htons(6001);
	addrTo.sin_addr.S_un.S_addr=inet_addr("192.168.1.100");
	while(TRUE)
	{
		retval=recvfrom(m_socket,recvBuf,200,0,(SOCKADDR*)&addrFrom,&len);
		if(SOCKET_ERROR==retval)
			break;
		for (int i=0; i<len; i++)
		{
			recvBuf[i] = toupper(recvBuf[i]);
		}
		printf(recvBuf);

		///////////////////////发送//////////////////////////
		len2 = strlen(recvBuf);
		sendto(m_socket,recvBuf,len2+1,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR));
		
	}

    //关闭socket
	closesocket(m_socket);
	WSACleanup();
	exit(0);
}
//2
#include<stdlib.h>
#include<string.h>
#include<winsock2.h>

#define BUFSIZE 256

void main(void)
{
	
	WSADATA wsaData;
	WSAStartup(0x0202,&wsaData);
	SOCKET m_socket;
	m_socket=socket(AF_INET,SOCK_DGRAM,0);
	if(INVALID_SOCKET==m_socket)
	{
		printf("套接字创建失败!");
	}
	SOCKADDR_IN addrSock;
	addrSock.sin_family=AF_INET;
	addrSock.sin_port=htons(6001);
	addrSock.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
	
	int retval;
	retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
	if(SOCKET_ERROR==retval)
	{
		closesocket(m_socket);
		printf("绑定失败!");
	}
	/////////////////接收///////////////////////////
	SOCKADDR_IN addrFrom;
	int len=sizeof(SOCKADDR),len2;
	
	char recvBuf[200];
	char tempBuf[300];
	SOCKADDR_IN addrTo;
	addrTo.sin_family=AF_INET;
	addrTo.sin_port=htons(6000);
		addrTo.sin_addr.S_un.S_addr=inet_addr("192.168.1.100");
	while(TRUE)
	{
       // scanf("%s",recvBuf);
		fgets(recvBuf,256,stdin);
		///////////////////////发送//////////////////////////
		len2 = strlen(recvBuf);
		sendto(m_socket,recvBuf,len2+1,0,
			(SOCKADDR*)&addrTo,sizeof(SOCKADDR));
		Sleep(1000);
		////////////////接收/////////////////////////////////
		retval=recvfrom(m_socket,recvBuf,200,0,(SOCKADDR*)&addrFrom,&len);
		if(SOCKET_ERROR==retval)
			break;
		printf(recvBuf);
	}
    //关闭socket
	closesocket(m_socket);
	WSACleanup();
	exit(0);
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-08-29 16:00  毛毛虫的薄刻  阅读(343)  评论(0编辑  收藏  举报