求C语言高手,实现一个简单的TCPIP程序以实现两台计算机之间的聊天通信,
1)两个程序,分别是客户端和服务器端
2)可以在客户端或服务器端输入文本信息,回车后将文本信息传输到对端,文本显示在对端屏幕上。
3)服务器可以与一个或多个客户端连接
要求:
1)使用C或C++语言
2)利用TCP socket 实现
3)可以在windows 或 Linux/unix下调试
4)不做界面,直接用显示在console 窗口上
5)必须有足够的注释
老师给了结构// tcpsever.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
void _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
int Port = 5150;
int Ret;
// Initialize Winsock version 2.2
if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function.
printf("WSAStartup failed with error %d\n", Ret);
return;
}
WSACleanup();
}
// tcpsample.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
void _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
int Ret;
// Initialize Winsock version 2.2
if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function.
printf("WSAStartup failed with error %d\n", Ret);
return;
}
感激不尽啊 展开
你上面给出的代码其实就是 MSDN 里面的演示代码,不过不完整,只演示了两个函数的使用,我给你看看我写的 TCP 通讯程序,可以在同一个局域网内的两台不同计算机之间聊天 :
这其实就是某本将网络通讯的教程里面的例子,不过是我自己重写了一遍,下面给你代码:
========================
下面是公共代码:
========================
#ifndef __CINITSOCK__H__
#define __CINITSOCK__H__
#include <winsock2.h>
#include <iphlpapi.h>
#pragma comment( lib, "ws2_32.lib" )
#pragma comment( lib, "iphlpapi.lib" )
class CInitSock
{
public:
CInitSock( int nMinorVer = 2, int nMajorVer = 2 )
{
WSADATA wsData;
WORD wVer = MAKEWORD( nMinorVer, nMajorVer );
if( 0 != WSAStartup( wVer, &wsData ) ) exit( 0 );
}
~CInitSock( )
{
WSACleanup( );
}
};
#endif
========================
下面是客户端的代码 :
=======================
#include "CInitSock.h"
#include <iostream>
using namespace std;
CInitSock g_Sock;
void main( )
{
SOCKET sockClient = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( INVALID_SOCKET == sockClient )
return;
sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons( 4567 );
sockAddr.sin_addr.S_un.S_addr = inet_addr( "127.0.0.1" );
if( -1 == connect( sockClient, ( sockaddr* )&sockAddr, sizeof( sockAddr ) ) )
{
cout << "connect failed" << endl;
return;
}
while( true )
{
char szBuf[ MAX_PATH ];
ZeroMemory( szBuf, sizeof( szBuf ) );
cout << "You Say : ";
cin >> szBuf;
if( SOCKET_ERROR == send( sockClient, szBuf, MAX_PATH, 0 ) )
{
cout << "send failed" << endl;
return;
}
int nRecvLen = recv( sockClient, szBuf, MAX_PATH, 0 );
if( nRecvLen > 0 )
{
// szBuf[ nRecvLen ] = '\0';
cout << "Service Say : " << szBuf << endl << endl;
}
else
{
cout << "recv failed" << endl;
return;
}
}
closesocket( sockClient );
}
======================
下面是服务端的代码:
======================
#include "CInitSock.h"
#include <iostream>
using namespace std;
CInitSock g_Sock;
void main( )
{
SOCKET sockClient = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( INVALID_SOCKET == sockClient )
return;
sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons( 4567 );
sockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
bind( sockClient, ( sockaddr* )&sockAddr, sizeof( sockAddr ) );
listen( sockClient, SOMAXCONN );
char szBuf[ MAX_PATH ];
sockaddr_in remoteAddr;
int nLen = sizeof( sockaddr_in );
SOCKET sock = accept( sockClient, ( sockaddr* )&remoteAddr, &nLen );
while( true )
{
int nRecvLen = recv( sock, szBuf, MAX_PATH, 0 );
if( nRecvLen > 0 )
{
cout << "Client Say : " << szBuf << endl << endl;
}
ZeroMemory( szBuf, sizeof( szBuf ) );
cout << "You Say : ";
cin >> szBuf;
if( SOCKET_ERROR == send( sock, szBuf, MAX_PATH, 0 ) )
{
cout << "send failed" << endl;
}
}
closesocket( sock );
closesocket( sockClient );
}
希望能够帮到楼主 。