0

我编写了一个小程序,可以从客户端 -> 服务器(发送)和服务器 -> 客户端(请求)发送文件。

这部分做得很好,但问题来了: 1. 我在服务器上找到了文件,我如何在客户端执行 cin?2. 如何强制服务器和客户端之间的消息同步?我的意思是我不希望服务器移动到下一步或冻结接收。例如(在这个问题中没有应用线程):- 服务器:等待来自客户端的消息。客户:发送消息。客户端:等待来自客户端的消息。服务器:发送消息。.....ETC。

在极少数情况下,消息按顺序到达,但在 99.999% 的情况下它们没有按顺序到达,并且两侧的程序都冻结了。

中序消息的问题是客户端的一个线程一直在阅读公司的回复,而不允许实际功能看到它们。

但是,关于第 1 点。我正在尝试使用此代码: 1. 没有共享资源,因此我尝试定义此函数中的所有内容(其中部分发生问题) 2. 我试图将此函数传递给线程所以服务器可以接受更多的客户端。3.发送和接收它们没有什么特别的,只是一个正常的发送/接收呼叫。3. 问:如果 SendMyMessage 和 ReceiveMyMessage 将被不同的线程使用,我应该将套接字与消息一起传递给它们吗?

void ExecuteRequest(void * x)
{

RequestInfo * req = (RequestInfo *) x;
        // 1st Message Direction get or put
fstream myFile;          
myFile.open(req->_fName);


char tmp;
string _MSG= "";
string cFile = "*";
if(req->_fDir.compare("put") == 0)
{
    if(myFile.is_open())
    {
        SendMyMessage("*F*");
        cFile = ReceiveMyMessage(); 
// I want here to ask the client what to do after he found the that file exist on the server, 
// I want the client to to get a message "*F*", then a cin command appear to him
// then the client enter a char 
// then a message sent back to the server
// then the server continue executing the code

//More code         
}

客户端:

{
cout <<"Waiting Message" <<endl;
    temps = ReceiveMessage();

    if(temps.compare("*F*") == 0)
    {
        cout <<"File found on Server want to:\n(1)Replace it.\n(2)Append to it." <<endl;
        cin>>temps;
        SendMyMessage(temps);
    }}

我正在使用 Visual Studio 2013 Windowx 7 线程正在使用:_beginthread(我删除了所有线程)

问候,

4

1 回答 1

0

在 linux 上,有一个系统调用“select”,服务器可以使用它等待打开的套接字。一旦有活动,比如客户端写了一些东西,服务器就会在那个套接字上唤醒并处理数据。

你在windows平台上。所以:http: //msdn.microsoft.com/en-us/library/windows/desktop/ms740141%28v=vs.85%29.aspx

于 2013-09-28T12:09:04.040 回答