首先
project->settings->link 在object/library modules 中加入ws2_32.lib文件
在项目上点右键->属性->配置属性->链接器->输入->附加依赖项加入ws2_32.lib
winSock2.h文件里的变量和函数的符号链接放在ws2_32.lib里
该库对应ws2_32.dll,提供了对网络相关API的支持,若使用其中的API,则应该将ws2_32.lib加入工程(否则需要动态载入ws2_32.dll)。
一收一发版:
client _easy.c
#include
#include
#include
int start_winsock()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup(wVersionRequested, &wsaData );
if ( err != 0 ) {
return -1;
}
if (LOBYTE( wsaData.wVersion ) != 1
|| HIBYTE(wsaData.wVersion) != 1 ) {
WSACleanup( );
return -1;
}
}
int set_conn_socket(SOCKET *sock)
{
int err;
char addr[100];
*sock = socket(AF_INET, SOCK_STREAM, 0);
//设置通信地址
SOCKADDR_IN addrSrv;
printf("请输入服务器的IP地址\n");
gets(addr);
addrSrv.sin_addr.S_un.S_addr = inet_addr(addr); // 本地回路地址是127.0.0.1;
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
//把socket与地址绑定在一起
err = connect(*sock, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (err != 0){
return -1;
}
}
int main()
{
int err;
char sendBuf[100], recvBuf[100];
SOCKET sockClient;
int ret;
if(start_winsock() == -1)
return 0;
err = set_conn_socket(&sockClient);
if(err == -1){
printf("连接失败,退出!");
} else
printf("连接成功,开始通信:\n");
while(1){
recv(sockClient, recvBuf, 100, 0);
printf("Says: %s\n", recvBuf); // 接收信息
gets(sendBuf);
//char sendBuf[100] = {"hello, I am here"};
send(sockClient, sendBuf, strlen(sendBuf)+1, 0);
}
}
server _easy.c
#include
#include
#include
int start_winsock()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup(wVersionRequested, &wsaData );
if ( err != 0 )
{
return -1;
}
if (LOBYTE( wsaData.wVersion ) != 1
|| HIBYTE(wsaData.wVersion) != 1 )
{
WSACleanup( );
return -1;
}
}
void set_socket(SOCKET *sock)
{
*sock = socket(AF_INET, SOCK_STREAM, 0);
//设置通信地址
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); // 将INADDR_ANY转换为网络字节序,调用 htonl(long型)或htons(整型)
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
//把socket与地址绑定在一起
bind(*sock, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
listen(*sock, 10);
}
void wait_conn(SOCKET *sockser, SOCKET *sockcli)
{
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
printf("正在等待客户连接!!\n");
*sockcli = accept(*sockser, (SOCKADDR*)&addrClient, &len);
printf("连接成功,开始通信:\n");
}
int main()
{
SOCKET sockSrv, sockConn;
char sendBuf[100], recvBuf[100];
int ret;
if(start_winsock() == -1)
return 0;
set_socket(&sockSrv);
wait_conn(&sockSrv,&sockConn);
while(1)
{
gets(sendBuf);
//char sendBuf[100] = {"hello, I am here"};
send(sockConn, sendBuf, strlen(sendBuf)+1, 0); // 发送显示欢迎信息
recv(sockConn, recvBuf, 100, 0);
printf("Says: %s\n", recvBuf); // 接收信息
}
}
多收多发版:
client.c
#include
#include
#include
int start_winsock()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup(wVersionRequested, &wsaData );
if ( err != 0 ) {
return -1;
}
if (LOBYTE( wsaData.wVersion ) != 1
|| HIBYTE(wsaData.wVersion) != 1 ) {
WSACleanup( );
return -1;
}
}
int set_conn_socket(SOCKET *sock)
{
int err;
char addr[100];
*sock = socket(AF_INET, SOCK_STREAM, 0);
//设置通信地址
SOCKADDR_IN addrSrv;
printf("请输入服务器的IP地址\n");
gets(addr);
addrSrv.sin_addr.S_un.S_addr = inet_addr(addr); // 本地回路地址是127.0.0.1;
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
//把socket与地址绑定在一起
err = connect(*sock, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (err != 0){
return -1;
}
}
int main()
{
int err;
char sendBuf[100], recvBuf[100];
SOCKET sockClient;
struct timeval timeout = {1,0};
struct fd_set rfds;
int ret;
if(start_winsock() == -1)
return 0;
err = set_conn_socket(&sockClient);
if(err == -1){
printf("连接失败,退出!");
} else
printf("连接成功,开始通信:\n");
while(1){
FD_ZERO(&rfds); /* 清空集合 */
FD_SET(sockClient, &rfds); /* 这里是添加检测 */
ret=select(0, &rfds, NULL, NULL, &timeout);
if(ret > 0)
{
if(FD_ISSET(sockClient,&rfds)) /* 这里检测的是fp在集合中是否状态变化,即可以操作。 */
{
recv(sockClient, recvBuf, 100, 0);
printf("Says: %s\n", recvBuf); // 接收信息
}
}
if(kbhit()) {
gets(sendBuf);
//char sendBuf[100] = {"hello, I am here"};
send(sockClient, sendBuf, strlen(sendBuf)+1, 0);
}
}
}
server.c
#include
#include
#include
int start_winsock()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup(wVersionRequested, &wsaData );
if ( err != 0 )
{
return -1;
}
if (LOBYTE( wsaData.wVersion ) != 1
|| HIBYTE(wsaData.wVersion) != 1 )
{
WSACleanup( );
return -1;
}
}
void set_socket(SOCKET *sock)
{
*sock = socket(AF_INET, SOCK_STREAM, 0);
//设置通信地址
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); // 将INADDR_ANY转换为网络字节序,调用 htonl(long型)或htons(整型)
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
//把socket与地址绑定在一起
bind(*sock, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
listen(*sock, 10);
}
void wait_conn(SOCKET *sockser, SOCKET *sockcli)
{
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
printf("正在等待客户连接!!\n");
*sockcli = accept(*sockser, (SOCKADDR*)&addrClient, &len);
printf("连接成功,开始通信:\n");
}
int main()
{
SOCKET sockSrv, sockConn;
char sendBuf[100], recvBuf[100];
struct timeval timeout = {1,0};
struct fd_set rfds;
int ret;
if(start_winsock() == -1)
return 0;
set_socket(&sockSrv);
wait_conn(&sockSrv,&sockConn);
while(1)
{
if(kbhit()) {
gets(sendBuf);
send(sockConn, sendBuf, strlen(sendBuf)+1, 0); // 发送显示欢迎信息
}
FD_ZERO(&rfds); /* 清空集合 */
FD_SET(sockConn, &rfds); /* 这里是添加检测 */
ret=select(0, &rfds, NULL, NULL, &timeout);
if(ret > 0)
{
if(FD_ISSET(sockConn,&rfds)) /* 这里检测的是fp在集合中是否状态变化,即可以操作。 */
{
ret = recv(sockConn, recvBuf, 100, 0);
if (ret > 0) //大于0才是有意义的数据,防止对方掉线打印无用信息
printf("Says: %s\n", recvBuf); // 接收信息
}
}
}
}