客户端:
// Final_Client.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <WINSOCK2.H>
#include <IOSTREAM>
#pragma comment(lib,"WS2_32.lib")
int main(int argc, char* argv[])
{
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;
}
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
//addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_addr.S_un.S_addr = inet_addr("10.13.29.25");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
char recvBuf[100];
recv(sockClient, recvBuf, 100, 0);
printf("%s
", recvBuf);
send(sockClient, "Attention: A Client has enter...
", strlen("Attention: A Client has enter...
")+1, 0);
while (true)
{
char talk[100];
printf("
Please enter what you want to say next("quit"to exit):");
gets(talk);
if (strcmp(talk,"quit") == 0)
{
printf("quit!
");
break;
}
send(sockClient, talk, strlen(talk)+1, 0);
char recvBuf[100];
recv(sockClient, recvBuf, 100, 0);
printf("%s Says: %s
", "Server", recvBuf);
}
printf("End linking...
");
closesocket(sockClient);
WSACleanup();
printf("
");
system("pause");
return 0;
}
服务器端:
// Final_Server.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <WINSOCK2.H>
#include <IOSTREAM>
#pragma comment(lib,"WS2_32.lib")
int main(int argc, char* argv[])
{
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;
}
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(6000);
bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
listen(sockSrv, 10);
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
while(true){
SOCKET sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len);
char sendBuf[100];
sprintf(sendBuf, "Welcome %s to the server program~
Now, let's start talking...
", inet_ntoa(addrClient.sin_addr));
send(sockConn, sendBuf, strlen(sendBuf)+1, 0);
char recvBuf[100];
recv(sockConn, recvBuf, 100, 0);
printf("%s
", recvBuf);
char * sockConnName = "Client";
while(true)
{
char recvBuf[100];
recv(sockConn, recvBuf, 100, 0);
printf("%s Says: %s
", sockConnName, recvBuf);
char talk[100];
printf("Please enter what you want to say next("quit"to exit):");
gets(talk);
if (strcmp(talk,"quit") == 0)
{
printf("quit!
");
break;
}
send(sockConn, talk, strlen(talk)+1, 0);
printf("
");
}
printf("
End talking...
");
closesocket(sockConn);
WSACleanup();
break;
}
printf("
");
system("pause");
return 0;
}