服务器端:
#include<cstring>
#include<iostream>
#include<winsock2.h>
void main()
{
WORD wVersionRequested;//要求的版本号
WSADATA wsaData;//that is to receive details of the Windows Sockets implementation.
int err;
/*
MAKEWORD(x,y)x是低字节这里表主版本号,y是高字节表次版本号
*/
wVersionRequested = MAKEWORD( 2, 2 );
/*
WSAStartup()加载相应的动态链接库并协商使用那个版本的socket
*/
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
SOCKET listenSocket = socket(AF_INET,SOCK_STREAM,0);
/*
struct sockaddr_in{
short sin_family;
unsigned short sin_port;
struct in_addr sin_addr;
char sin_zero[8];};
*/
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8080);
bind(listenSocket,(SOCKADDR *) &addrSrv,sizeof (SOCKADDR));
listen(listenSocket,5);
SOCKADDR addClient ;
int len = sizeof (SOCKADDR);
SOCKET sockConn = accept(listenSocket,&addClient,&len);
while(1)
{
char sendBuff[100];
std::cin>>sendBuff;
send(sockConn,sendBuff,strlen(sendBuff)+1,0);
char recBuff[100];
recv(sockConn,recBuff,sizeof(recBuff),0);
std::cout<<recBuff<<std::endl;
}
}
客服端:
#include<cstring>
#include<iostream>
#include<winsock2.h>//包含socket用到的函数
#pragma comment(lib,"ws2_32.lib")//加入动态链接库
void main()
{
WORD wVersionRequested;//要求的版本号
WSADATA wsaData;//that is to receive details of the Windows Sockets implementation.
int err;
/*
MAKEWORD(x,y)x是低字节这里表主版本号,y是高字节表次版本号
*/
wVersionRequested = MAKEWORD( 2, 2 );
/*
WSAStartup()加载相应的动态链接库并协商使用那个版本的socket
*/
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
//创建一个客服端socket
SOCKET clientSocket = socket(AF_INET,SOCK_STREAM,0);
//addrSev记录主机的ip和端口信息,只有其sin_family元素网络字节顺序不需要转换
SOCKADDR_IN addrSev;
addrSev.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSev.sin_family = AF_INET;
//转换
addrSev.sin_port = htons(8080);
connect(clientSocket,(SOCKADDR *) &addrSev,sizeof (SOCKADDR));
while(1)
{
char recvBuf[100];
recv(clientSocket,recvBuf,100,0);
std::cout<<recvBuf<<std::endl;
char sendBuf[100];
std::cin>>sendBuf;
send(clientSocket,sendBuf,strlen(sendBuf)+1,0);
}
}
#include<cstring>
#include<iostream>
#include<winsock2.h>
void main()
{
WORD wVersionRequested;//要求的版本号
WSADATA wsaData;//that is to receive details of the Windows Sockets implementation.
int err;
/*
MAKEWORD(x,y)x是低字节这里表主版本号,y是高字节表次版本号
*/
wVersionRequested = MAKEWORD( 2, 2 );
/*
WSAStartup()加载相应的动态链接库并协商使用那个版本的socket
*/
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
SOCKET listenSocket = socket(AF_INET,SOCK_STREAM,0);
/*
struct sockaddr_in{
short sin_family;
unsigned short sin_port;
struct in_addr sin_addr;
char sin_zero[8];};
*/
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8080);
bind(listenSocket,(SOCKADDR *) &addrSrv,sizeof (SOCKADDR));
listen(listenSocket,5);
SOCKADDR addClient ;
int len = sizeof (SOCKADDR);
SOCKET sockConn = accept(listenSocket,&addClient,&len);
while(1)
{
char sendBuff[100];
std::cin>>sendBuff;
send(sockConn,sendBuff,strlen(sendBuff)+1,0);
char recBuff[100];
recv(sockConn,recBuff,sizeof(recBuff),0);
std::cout<<recBuff<<std::endl;
}
}
客服端:
#include<cstring>
#include<iostream>
#include<winsock2.h>//包含socket用到的函数
#pragma comment(lib,"ws2_32.lib")//加入动态链接库
void main()
{
WORD wVersionRequested;//要求的版本号
WSADATA wsaData;//that is to receive details of the Windows Sockets implementation.
int err;
/*
MAKEWORD(x,y)x是低字节这里表主版本号,y是高字节表次版本号
*/
wVersionRequested = MAKEWORD( 2, 2 );
/*
WSAStartup()加载相应的动态链接库并协商使用那个版本的socket
*/
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
//创建一个客服端socket
SOCKET clientSocket = socket(AF_INET,SOCK_STREAM,0);
//addrSev记录主机的ip和端口信息,只有其sin_family元素网络字节顺序不需要转换
SOCKADDR_IN addrSev;
addrSev.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSev.sin_family = AF_INET;
//转换
addrSev.sin_port = htons(8080);
connect(clientSocket,(SOCKADDR *) &addrSev,sizeof (SOCKADDR));
while(1)
{
char recvBuf[100];
recv(clientSocket,recvBuf,100,0);
std::cout<<recvBuf<<std::endl;
char sendBuf[100];
std::cin>>sendBuf;
send(clientSocket,sendBuf,strlen(sendBuf)+1,0);
}
}