在同一台计算机上运行客户端/服务器 [英] Running Client/Server on the same machine

查看:465
本文介绍了在同一台计算机上运行客户端/服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试在同一台计算机上运行客户端/服务器,其中包含两个(.CPP)文件,一个用于服务器和另一个用于客户端。但是发生的情况是服务器一直工作直到listen命令,并且无法访问客户端代码。尽管如此,我确定代码是正确的。

SERVER代码是:

Hi,

I''m trying to run a client/server on the same machine, with two (.CPP) files, one for the server and the other for the client.But what happens is that the server works up until the listen command, and the client code cannot be reached. Eventhough, i''m sure that the code is correct.
The SERVER code is:

#include <winsock2.h>
#include <iostream>

using namespace std;

int main()
{
    cout <<"Server: Part 2a\n";
    cout <<"CelestialKey's Tutorial\n\n";

    int error;
    WSAData wsaData;

    error = WSAStartup(MAKEWORD(2, 2), &wsaData);

    if (error == SOCKET_ERROR)
    {
        cout << "Server: Winsock Failed to begin!\n";
        return 1;
    }

    cout << "Server: WinSocket Started Correctly!\n";
    int mySocket = socket(AF_INET, SOCK_STREAM, 0);

    if (mySocket == SOCKET_ERROR)
    {
        cout << "Server: Socket Failed to initialize!\n";
    }

    cout << "Server: Socket Opened Properly!\n";

    struct sockaddr_in server; // New! Our network socket structure

    server.sin_family = AF_INET; // Always have ti as this for internet
    server.sin_port = htons(7654); // Dont forget the byte ordering
    server.sin_addr.s_addr = INADDR_ANY; // Lets us accept a conenction on ANY internet

    error = bind(mySocket, (sockaddr*)&server, sizeof(server)); // Attempt to bind the socket

    if (error == SOCKET_ERROR)
    {
        cout << "Server: Ah ****... Somethings wrong with binding\n";
    }


    error = listen(mySocket, 5); // Listen for connections

    if (error == SOCKET_ERROR)
    {
        cout << "Server: Too deaf to listen...\n"; // Did we error?!?!
    }

    cout << "Server: Waiting for a client to connect ...\n"; // Just to keep us up to date - ish

    int clientSocket; // Used for the client

    clientSocket = accept(mySocket, 0, 0); // Accepts the client

    if (clientSocket == SOCKET_ERROR)
    {
        cout << "I DUN WANNA!\n"; // did we Fu** up again?
    }

    cout << "Server: Client Connected!\n";

    closesocket(mySocket);

    cout << "Server: Socket Closed\n";

    WSACleanup();

    cout << "Server: WinSocket Shutdown\n";

    cout << "Server: Press any key to shutdown server ...";
    getchar();
    
    return 0;
}





客户代码是:



The Client code is:

#include <winsock2.h>
#include <iostream>

using namespace std;


int main()
{
    cout << "Client: Part 2b!\n";
    cout << "Client: CelestialKey's Tutorial\n\n";

    int error;
    WSAData wsaData;

    error = WSAStartup(MAKEWORD(2, 2), &wsaData);

    if (error == SOCKET_ERROR)
    {
        cout << "Client: WinSock Hates me!!!\n";
        return 1;
    }

    cout << "Client: WinSocket Loaded.\n";

    int mySocket = socket(AF_INET, SOCK_STREAM, 0);

    if (mySocket == SOCKET_ERROR)
    {
        cout << "Client: Socket Door is stuck.. Wont open.. -.-;\n";
    }

    cout << "Client: Socket Opened Successfully!\n";


    char server_name[40] = "localhost"; // The server name we will connect to

    // If you are on a network and you want to run the client on another computer, you can get the IP from
    // running "ipconfig" from the command line, and enter that IP address instead of the word "localhost".

    struct hostent *host_entry; // Translates into something the computer can understand

    // The hostent struct contains the ip and anme of the host (Among other things). We can fill it by calling the gethostbyname();

    host_entry = gethostbyname(server_name); // Gather out information

    if (host_entry == NULL)
    {
        cout << "Client: Ah ****, im lost, wheres the host at?\n";
    }

    
    struct sockaddr_in server;

    server.sin_family = AF_INET;
    server.sin_port = htons((unsigned short) 7654);
    server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;


    error = connect(mySocket, (sockaddr*)&server, sizeof(server)); // Lets attempt to connect now

    if (error == SOCKET_ERROR)
    {
        cout << "Client: Damn server wont let me connect!\n";
    }

    

    cout << "Client: We are connected!!!\n";
    closesocket(mySocket);

    cout << "Client: Closing Socket.\n";
    WSACleanup();

    cout << "Client: Winsock has been shut down.\n";

    cout << "Press any key to close down Client ...\n";
    getchar();
    
    return 0;
}

我认为这是我附加文件或创建项目的方式!!

我正在使用C ++ Builder 6 ,使用控制台向导。有人可以告诉我这个程序的工作是什么 确切的步骤



祝你好运。

Rania

I think it''s the way i''m attaching the files, or creating the project!!
I''m using C++ Builder 6, using the console wizard. can somebody please tell me what are the EXACT STEPS for making this program work??

Best regards.
Rania

推荐答案

尝试制作两个不同的项目,一个用于客户端,另一个用于服务器。



为客户检查此代码:

http://www.win32developer.com/tutorial/winsock/winsock_tutorial_3.shtm [ ^ ]



和服务器:

http://www.win32developer.com/tutorial/winsock/winsock_tutorial_4.shtm [ ^ ]



我希望他会告诉你客户端和服务器连接的基本信息。



祝你好运!
Try to make two different projects, one for client and another one for server.

for client check this code:
http://www.win32developer.com/tutorial/winsock/winsock_tutorial_3.shtm[^]

and for server:
http://www.win32developer.com/tutorial/winsock/winsock_tutorial_4.shtm[^]

I hope this will tell you the basic of connectivity of client and server.

Good Luck!


这篇关于在同一台计算机上运行客户端/服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆