sercer端
#include <stdio.h>
#include <process.h>
#include <Winsock2.h>
#pragma comment(lib,"ws2_32.lib")
void send(void *a);
void showIP();
SOCKET sockConn;
SOCKADDR_IN addrClient;//连接上的客户端IP地址
void main()
{
printf("server's information\n");
showIP();
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1,1);
err = WSAStartup(wVersionRequested,&wsaData);
if ( err != 0 )
{
return;
}
if (LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup();
return;
}
//构造监听SOCKET,流式SOCKET
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
//配置监听地址和端口:
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6003);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//绑定端口
listen(sockSrv,5);
int len=sizeof(SOCKADDR);
sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len); //接受客户端连接,获取客户端的IP地址
_beginthread(send,0,NULL);
char recvBuf[50];
while(1)
{
//char sendBuf[50];
//char sendMes[50];
//scanf("%s",sendMes);
//printf("-->:%s\n",sendMes);
//sprintf(sendBuf,sendMes,inet_ntoa(addrClient.sin_addr));//组合消息发送出去
//send(sockConn,sendBuf,strlen(sendBuf)+1,0);//发送消息到客户端
//
recv(sockConn,recvBuf,50,0);//接受客户端消息
printf("<--:%s\n",recvBuf);
}
closesocket(sockConn);//断开连接
}
void send(void *a)
{
char sendBuf[50];
char sendMes[50];
while (1)
{
gets(sendMes);
printf("-->:%s\n",sendMes);
sprintf(sendBuf,sendMes,inet_ntoa(addrClient.sin_addr));//组合消息发送出去
send(sockConn,sendBuf,strlen(sendBuf)+1,0);//发送消息到客户端
}
_endthread();
}
void showIP() //定义CheckIP()函数,用于获取本机IP地址
{
WSADATA wsaData;
char name[20];//定义用于存放获得的主机名的变量
char *ip;//定义IP地址变量
PHOSTENT hostinfo;
//调用MAKEWORD()获得Winsock版本的正确值,用于加载Winsock库
if ( WSAStartup( MAKEWORD(2,0), &wsaData ) == 0 ) {
//现在是加载Winsock库,如果WSAStartup()函数返回值为0,说明加载成功,程序可以继续
if( gethostname ( name, sizeof(name)) == 0) {
//如果成功地将本地主机名存放入由name参数指定的缓冲区中
if((hostinfo = gethostbyname(name)) != NULL) {
//这是获取主机名,如果获得主机名成功的话,将返回一个指针,指向hostinfo,hostinfo
//为PHOSTENT型的变量,下面即将用到这个结构体
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
//调用inet_ntoa()函数,将hostinfo结构变量中的h_addr_list转化为标准的点分表示的IP
//地址(如192.168.0.1)
printf("%s\n",ip);//输出IP地址
printf("%s\n",name);
}
}
WSACleanup();//卸载Winsock库,并释放所有资源
}
}
client端
#include <stdio.h>
#include <Winsock2.h>
#include <process.h>
#pragma comment(lib, "ws2_32.lib")
SOCKET sockClient;
void send(void *p);
void main()
{
WORD wVersionRequested;
WSADATA wsaData;//WSAata用来存储系统传回的关于WinSocket的信息
int err;
wVersionRequested = MAKEWORD(1,1);
err = WSAStartup( wVersionRequested, &wsaData);
if ( err != 0 )
{
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup();
return;
}
sockClient=socket(AF_INET,SOCK_STREAM,0);//AF_INET表示TCP连接 //初始化连接与端口号
SOCKADDR_IN addrSrv;
char IP[20];
printf("Please input server's IP: ");
scanf("%s",IP);
addrSrv.sin_addr.S_un.S_addr=inet_addr(IP);//本机地址,服务器在本机开启
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6003);//设置端口号
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//连接服务器
int hand = _beginthread(send,0,NULL);
char recvBuf[50];
while (1)
{
//char sendMes[50];
//scanf("%s",sendMes);
recv(sockClient,recvBuf,50,0);//接受数据
printf("<--:%s\n",recvBuf);
//printf("-->:%s\n",sendMes);
//send(sockClient,sendMes,strlen(sendMes)+1,0);//发送数据
}
closesocket(sockClient);//关闭连接
WSACleanup();
system("pause");
}
void send(void *p)
{
char sendMes[50];
while (1)
{
gets(sendMes);
printf("-->:%s\n",sendMes);
send(sockClient,sendMes,strlen(sendMes)+1,0);//发送数据
}
_endthread();
}