服务端:
// Server.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
//告诉连接器与ws2.32库连接
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
//初始化ws2_32.dll
WSADATA wsaData;
WORD wSockVersion = MAKEWORD(2,0);
::WSAStartup(wSockVersion,&wsaData);
//创建套接字
SOCKET s = ::socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(s == INVALID_SOCKET)
{
printf("Failed socket./n");
::WSACleanup();
return 0;
}
//填充sockaddr_in结构
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
//绑定
if(::bind(s,(LPSOCKADDR)&sin,sizeof(sin)) == SOCKET_ERROR)
{
printf("Failed bind./n");
::WSACleanup();
return 0;
}
//监听
if(::listen(s,2) == SOCKET_ERROR)
{
printf("Failed listen./n");
return 0;
}
//循环接受客户的连接请求
sockaddr_in remoteAddr;
int nAddlen = sizeof(remoteAddr);
SOCKET client;
char szText[] = "10Server Demo!/r/n";
while(TRUE)
{
//接收新连接
client = ::accept(s,(SOCKADDR*)&remoteAddr,&nAddlen);
if(client == INVALID_SOCKET)
{
printf("Failed accept()./n");
continue;
}
printf("接受到一个连接:%s/r/n",inet_ntoa(remoteAddr.sin_addr));
//向客户端发送数据
::send(client,szText,strlen(szText),0);
//关闭同客户端的连接
::closesocket(client);
}
//关闭
::closesocket(s);
//释放ws2_32库
::WSACleanup();
return 0;
}
客户端:
// Client.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
WSADATA wsaData;
WORD wSockVersion = MAKEWORD(2,0);
::WSAStartup(wSockVersion,&wsaData);
//创建套接字
SOCKET s = ::socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(s == INVALID_SOCKET)
{
printf("Failed socket()./n");
::WSACleanup();
return 0;
}
//
sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(8888);
servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if(::connect(s,(sockaddr*)&servAddr,sizeof(servAddr)) == -1)
{
printf("Failed connect()./n");
::WSACleanup();
return 0;
}
char buff[256];
int nRecv = ::recv(s,buff,256,0);
if(nRecv > 0)
{
printf("接收到数据:%s",buff);
}
::closesocket(s); //关闭
::WSACleanup();
return 0;
}
用VC6。0编译后进行测试,客户端成功连上服务端,服务端记录下客户端的IP。
以上C代码由于用到了一些别的东西,所以如果保存为。C,用VC是无法编译的,如果想既保存为。C,又编译通过,需要改成如下:
服务端:
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
WSADATA wsaData;
SOCKET s;
struct sockaddr_in servAddr;
char buff[256];
int nRecv;
WORD wSockVersion = MAKEWORD(2,0);
WSAStartup(wSockVersion,&wsaData);
//创建套接字
s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(s == INVALID_SOCKET)
{
printf("Failed socket()./n");
WSACleanup();
return 0;
}
//
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(8888);
servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if(connect(s,(struct sockaddr*)&servAddr,sizeof(servAddr)) == -1)
{
printf("Failed connect()./n");
WSACleanup();
return 0;
}
nRecv = recv(s,buff,256,0);
if(nRecv > 0)
{
buff[nRecv] = '/0';
printf("接收到数据:%s",buff);
}
closesocket(s); //关闭
WSACleanup();
return 0;
}
客户端:
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
WSADATA wsaData;
SOCKET s;
struct sockaddr_in servAddr;
char buff[256];
int nRecv;
WORD wSockVersion = MAKEWORD(2,0);
WSAStartup(wSockVersion,&wsaData);
//创建套接字
s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(s == INVALID_SOCKET)
{
printf("Failed socket()./n");
WSACleanup();
return 0;
}
//
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(8888);
servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if(connect(s,(struct sockaddr*)&servAddr,sizeof(servAddr)) == -1)
{
printf("Failed connect()./n");
WSACleanup();
return 0;
}
nRecv = recv(s,buff,256,0);
if(nRecv > 0)
{
buff[nRecv] = '/0';
printf("接收到数据:%s",buff);
}
closesocket(s); //关闭
WSACleanup();
return 0;
}