新建Win32 Console Project,工程名tcp,空工程,新建文件tcp.cpp,粘贴以下代码。
在Project Settings的C/C++页面的Category下拉框中选择Code Generation,选择Use Run-time library:Debug Multithread。在Link页面的Catagory下拉框中选择General,在Object/library moudules后面增加ws2_32.lib。注意前面加个空格。
编译运行后再打开一个dos窗口输入telnet 127.0.0.1 20248。
//
tcp.cpp : Defines the entry point for the console application.
//
#include < afxwin.h >
#include < stdio.h >
#include < WINSOCK2.H >
UINT ServerThread(LPVOID pParam);
int main( int argc, char * argv[])
... {
int nResCode = 0;
printf("Press "a" to terminate program ");
AfxBeginThread(ServerThread,0);
do
...{
} while (getchar() != 'a');
return nResCode;
}
UINT ServerThread(LPVOID pParam)
... {
printf("Starting up TCP server ");
//WSAStartup初始化WinSock
//第一个参数指定允许使用的WinSock规范的最高版本
WSADATA wsaData;
int wsaret = WSAStartup(MAKEWORD(1,1),&wsaData);
if (wsaret != 0)
...{
return 0;
}
sockaddr_in local;
local.sin_family = AF_INET; //地址族
local.sin_addr.s_addr = INADDR_ANY; //网际IP地址
local.sin_port = htons((u_short)20248); //使用的端口,转换为网络序
//创建SOCKET
SOCKET server;
server = socket(AF_INET, SOCK_STREAM, 0);
if (server == INVALID_SOCKET)
...{
return 0;
}
//将刚创建的套接字和sockaddr_in结构联系起来
//它主要使用本地地址及一个特定的端口来连接套接字
if (bind(server,(sockaddr*)&local,sizeof(local)) != 0)
...{
return 0;
}
//listen侦听来自客户端的连接
//第二个参数表示最大连接数
if (listen(server,10) != 0)
...{
return 0;
}
//客户端套接字
SOCKET client;
sockaddr_in from;
int fromlen = sizeof(from);
while (true)
...{
char buffer[512];
char* cServerIP = inet_ntoa(local.sin_addr);
char* cClientIP = inet_ntoa(from.sin_addr);
//接收客户端连接
client = accept(server, (struct sockaddr*)&from,&fromlen);
sprintf(buffer,"Congratulation! Your IP is %s ",cClientIP);
//向客户端发送字符串
send(client, buffer, strlen(buffer),0);
printf("Connection from %s ",cClientIP);
//发送信息
int i = 0;
for (i = 10;i>0;)
...{
sprintf(buffer," ");
send(client,buffer,strlen(buffer),0);
sprintf(buffer,"%d ",i--);
send(client,buffer,strlen(buffer),0);
Sleep(1000);
}
//关闭客户端连接
sprintf(buffer,"Terminate %s by %s ",
cClientIP,
cServerIP);
printf(buffer);
send(client, buffer, strlen(buffer),0);
closesocket(client);
}
closesocket(server);
//终止对WinSock的调用
WSACleanup();
return 0;
}
//
#include < afxwin.h >
#include < stdio.h >
#include < WINSOCK2.H >
UINT ServerThread(LPVOID pParam);
int main( int argc, char * argv[])
... {
int nResCode = 0;
printf("Press "a" to terminate program ");
AfxBeginThread(ServerThread,0);
do
...{
} while (getchar() != 'a');
return nResCode;
}
UINT ServerThread(LPVOID pParam)
... {
printf("Starting up TCP server ");
//WSAStartup初始化WinSock
//第一个参数指定允许使用的WinSock规范的最高版本
WSADATA wsaData;
int wsaret = WSAStartup(MAKEWORD(1,1),&wsaData);
if (wsaret != 0)
...{
return 0;
}
sockaddr_in local;
local.sin_family = AF_INET; //地址族
local.sin_addr.s_addr = INADDR_ANY; //网际IP地址
local.sin_port = htons((u_short)20248); //使用的端口,转换为网络序
//创建SOCKET
SOCKET server;
server = socket(AF_INET, SOCK_STREAM, 0);
if (server == INVALID_SOCKET)
...{
return 0;
}
//将刚创建的套接字和sockaddr_in结构联系起来
//它主要使用本地地址及一个特定的端口来连接套接字
if (bind(server,(sockaddr*)&local,sizeof(local)) != 0)
...{
return 0;
}
//listen侦听来自客户端的连接
//第二个参数表示最大连接数
if (listen(server,10) != 0)
...{
return 0;
}
//客户端套接字
SOCKET client;
sockaddr_in from;
int fromlen = sizeof(from);
while (true)
...{
char buffer[512];
char* cServerIP = inet_ntoa(local.sin_addr);
char* cClientIP = inet_ntoa(from.sin_addr);
//接收客户端连接
client = accept(server, (struct sockaddr*)&from,&fromlen);
sprintf(buffer,"Congratulation! Your IP is %s ",cClientIP);
//向客户端发送字符串
send(client, buffer, strlen(buffer),0);
printf("Connection from %s ",cClientIP);
//发送信息
int i = 0;
for (i = 10;i>0;)
...{
sprintf(buffer," ");
send(client,buffer,strlen(buffer),0);
sprintf(buffer,"%d ",i--);
send(client,buffer,strlen(buffer),0);
Sleep(1000);
}
//关闭客户端连接
sprintf(buffer,"Terminate %s by %s ",
cClientIP,
cServerIP);
printf(buffer);
send(client, buffer, strlen(buffer),0);
closesocket(client);
}
closesocket(server);
//终止对WinSock的调用
WSACleanup();
return 0;
}