TCP
// TCP_Server.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <winsock2.h> #include <stdio.h> #pragma comment (lib,"ws2_32.lib") using namespace std; void _tmain(int argc, _TCHAR* argv[]) { WSADATA wsData; //WinSock Async 环境数据 WSAStartup(0x0202, &wsData); //启动套接字环境 SOCKADDR_IN ServerAddr; //服务器地址 SOCKADDR_IN ClientAddr; //客户端地址 SOCKET ListenSocket; //用于监听的套接字 SOCKET ClientSocket; //连接套接字 //1、建立套接字 ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if( ListenSocket == INVALID_SOCKET ) { WSACleanup(); return; } //2、绑定到指定的端口 memset(&ServerAddr, 0, sizeof(ServerAddr)); ServerAddr.sin_family = AF_INET; //地址组:网间网络地址 ServerAddr.sin_port = htons(9000); //端口号转换成网络字节顺序 ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); bind(ListenSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)); //绑定 //3、监听 listen(ListenSocket, 5); //在队列中允许5个客户端的请求 //4、接受连接 int AddrLen = sizeof(ClientAddr); ClientSocket = accept(ListenSocket, (SOCKADDR*)&ClientAddr, &AddrLen); if( ClientSocket == INVALID_SOCKET ) { closesocket(ListenSocket); WSACleanup(); return; } //5进入收发数据的循环 char Buffer[1024]; int nBytes; while( 0 < ( nBytes = recv(ClientSocket, Buffer, sizeof(Buffer), 0) ) ) { //接收数据 Buffer[nBytes] = 0; printf("Client:%s ", Buffer); if( Buffer[0] == '~' ) break; //发送数据 //char BufferBack[1024]; scanf("%s",Buffer); send(ClientSocket, Buffer, strlen(Buffer)+1, 0); } //6.关闭套接字 //shutdown( ClientSocket, SD_SEND ); //Sleep(1000);//1秒时间足够发送关闭消息 closesocket(ListenSocket);//这里直接关闭 closesocket(ClientSocket); WSACleanup(); //关闭套接字环境 }
// TCP_Client.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; #include <winsock2.h> #include <stdio.h> #pragma comment (lib,"ws2_32.lib") /////////////////////////////////////////////////////////////////////////// // TCP客户端 // 启动 -> 建立套接字 -> 连接 -> 数据收发 -> 关闭套接字 /////////////////////////////////////////////////////////////////////////// void main() { WSADATA wsData; WSAStartup(0x0202, &wsData); //启动套接字 //1.建立套接字 SOCKET ClientSocket; //连接套接字 ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //ClientSocket初始化 if( ClientSocket == INVALID_SOCKET ) //判断是否初始化成功 { WSACleanup(); return; } //2[连接到制定的服务器端口] SOCKADDR_IN ServerAddr; //服务器地址 memset(&ServerAddr, 0, sizeof(ServerAddr)); ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons(9000); // host to net 主机转化到网络 ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); connect(ClientSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)); //绑定 //3.进入收发数据的循环 int nRcv; char Buffer[1024]; for(;;) { //发送数据 scanf("%s",Buffer); send(ClientSocket,Buffer,strlen(Buffer)+1,0); //接收数据 if(0 >= (nRcv = recv(ClientSocket, Buffer, sizeof(Buffer), 0))) { break; } printf("Server:%s ",Buffer); } //4.关闭套接字 shutdown(ClientSocket, SD_SEND); closesocket(ClientSocket); WSACleanup(); //关闭套接字环境 }
UDP
// UDP_Test_Server.cpp : 定义控制台应用程序的入口点。 // // Server_1016.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> using namespace std; #include <winsock2.h> #include <stdio.h> #pragma comment (lib,"ws2_32.lib") #include <ws2tcpip.h> int main() { WSADATA wsaData; WORD wVersion = MAKEWORD(2,2); WSAStartup(wVersion,&wsaData); SOCKET sServer = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); //set host INFor sockaddr_in ServerAddr; ServerAddr.sin_family=AF_INET; ServerAddr.sin_port=htons(4567); ServerAddr.sin_addr.S_un.S_addr = INADDR_ANY; //接收群消息 ip_mreq mcast; ZeroMemory(&mcast,sizeof(mcast)); mcast.imr_interface.S_un.S_addr = INADDR_ANY; mcast.imr_multiaddr.S_un.S_addr = inet_addr("234.5.6.7"); int iRet = ::setsockopt(sServer,IPPROTO_IP,IP_ADD_MEMBERSHIP, (char*)&mcast,sizeof(mcast)); //绑定 if(::bind(sServer, (LPSOCKADDR)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR) { return 0; } //recrive message char szBuf[1024]; sockaddr_in addr; int nLen = sizeof(addr); while(true) //接收数据 { int nRecv = ::recvfrom(sServer,szBuf,1024,0,(sockaddr*)&addr,&nLen); if(nRecv > 0) { szBuf[nRecv] = '