我测试一个小的通信程序一个服务器(Server.cpp),一个客户端(Client.cpp),编译成两个可执行文件后,先执行服务器,再执行客户端,可为什么编译通过,而没有实现任何功能啊?谢谢!
// Server.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "Server.h"
#include "winsock.h"
#include "afxsock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if(!AfxSocketInit())
{
printf("Windows Socket initial failed! ");
return nRetCode;
} //套接字初始化
CSocket ChatSend,Server;//构造2个socket对象
unsigned int nPort=3000;//端口号
char strAddr[]="127.0.0.1";//服务器端的IP地址
char csSendText[100];
int csCount;
if(!ChatSend.Create(nPort)) //初始化socket
{
printf("SendSocket create failed! ");
return nRetCode;
}
else
{
ChatSend.Bind(nPort,strAddr);
//把本地址和socket绑定
ChatSend.Listen(); //开始侦听
ChatSend.Accept(Server);
//在新的套接字上接收客户端socket连接
}
printf("Please input string to send: ");
gets(csSendText);
csCount=strlen(csSendText);
while(csCount>0)
{
Server.SendTo(csSendText,csCount,nPort,strAddr);
printf("Please input string to send: ");
gets(csSendText);
csCount=strlen(csSendText);
} //接受输入,并将其发送
Server.Close(); //关闭这2个socket
ChatSend.Close();
return nRetCode;
}// Client.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include "Client.h"
#include "winsock.h"
#include "afxsock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if(!AfxSocketInit())
{
printf("WindowsSocket initial failed!");
return nRetCode;
} //套接字初始化
CSocket ChatReceive; //构造一个socket对象
unsigned int nPort=3000;
//端口号,必须与服务器端一致
CString strAddr="127.0.0.1";
//服务器端IP地址
char csReceiveText[100];
int csCount=100;
memset(csReceiveText,0,100);
if(!ChatReceive.Create(nPort))
{
printf("ReceiveSocket create failed! ");
return nRetCode;
}
else
{
ChatReceive.Connect(strAddr,nPort);
} //建立和服务器端的连接
while(1)
{
ChatReceive.ReceiveFrom(csReceiveText,csCount,strAddr,nPort);
puts(csReceiveText);
memset(csReceiveText,0,100);
} //接收字符串,输出
ChatReceive.Close(); //关闭socket
return nRetCode;
}
// Server.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "Server.h"
#include "winsock.h"
#include "afxsock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if(!AfxSocketInit())
{
printf("Windows Socket initial failed! ");
return nRetCode;
} //套接字初始化
CSocket ChatSend,Server;//构造2个socket对象
unsigned int nPort=3000;//端口号
char strAddr[]="127.0.0.1";//服务器端的IP地址
char csSendText[100];
int csCount;
if(!ChatSend.Create(nPort)) //初始化socket
{
printf("SendSocket create failed! ");
return nRetCode;
}
else
{
ChatSend.Bind(nPort,strAddr);
//把本地址和socket绑定
ChatSend.Listen(); //开始侦听
ChatSend.Accept(Server);
//在新的套接字上接收客户端socket连接
}
printf("Please input string to send: ");
gets(csSendText);
csCount=strlen(csSendText);
while(csCount>0)
{
Server.SendTo(csSendText,csCount,nPort,strAddr);
printf("Please input string to send: ");
gets(csSendText);
csCount=strlen(csSendText);
} //接受输入,并将其发送
Server.Close(); //关闭这2个socket
ChatSend.Close();
return nRetCode;
}// Client.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include "Client.h"
#include "winsock.h"
#include "afxsock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// The one and only application objectCWinApp theApp;using namespace std;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if(!AfxSocketInit())
{
printf("WindowsSocket initial failed!");
return nRetCode;
} //套接字初始化
CSocket ChatReceive; //构造一个socket对象
unsigned int nPort=3000;
//端口号,必须与服务器端一致
CString strAddr="127.0.0.1";
//服务器端IP地址
char csReceiveText[100];
int csCount=100;
memset(csReceiveText,0,100);
if(!ChatReceive.Create(nPort))
{
printf("ReceiveSocket create failed! ");
return nRetCode;
}
else
{
ChatReceive.Connect(strAddr,nPort);
} //建立和服务器端的连接
while(1)
{
ChatReceive.ReceiveFrom(csReceiveText,csCount,strAddr,nPort);
puts(csReceiveText);
memset(csReceiveText,0,100);
} //接收字符串,输出
ChatReceive.Close(); //关闭socket
return nRetCode;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货