u011718414
自言自语ing
2015-08-02 03:04

linux虚拟机与windows间socket通信问题

  • 虚拟机
  • 通信
  • linux
  • socket

虚拟机linux服务端 windows客户端 虚拟机桥接模式,能互相ping到,但运行连接不上
linux Server
#include
#include
#include
#include
#include
#include
#include
#include

#define BACKLOG 5

int main(int arge,char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
int data[2];
int sum ;
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket fail");
exit(1);
}

my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(12345);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(my_addr.sin_zero),0);

if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1)
{
    perror("bind fail");
    exit(1);
}
if(listen(sockfd,BACKLOG) ==-1)
{
    perror("listen fail");
    exit(1);
}

while(1)
{
    sin_size  = sizeof(struct sockaddr_in);
    printf("hello\n");
    if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size)) == -1)
    {
        perror("accept fail");
        continue;
    }
    printf("Server:got connection from %s\n",inet_ntoa(their_addr.sin_addr));
    if(!fork())
    {
        while(1)
        {
            if((recv(new_fd,data,sizeof(data),0)) == -1)
            {
                printf("recv fail");
                break;
            }
            sum = data[0] +data[1];
            send(new_fd,(const char*)(char*)&sum,sizeof(sum),0);


        }
        close(new_fd);
        exit(0);
    }



}
close(new_fd);
close(sockfd);

return 0;

}

windows客户端
// Client.cpp : 定义控制台应用程序的入口点。
//

//#include "stdafx.h"
#include
#include
#pragma comment(lib, "ws2_32.lib")

using namespace std;

int main(int argc, char* argv[])
{
try
{
//初始化套接字库
//WSAStartup
WORD wVersion = MAKEWORD(2, 2);
WSADATA wsaData;
if (0 != ::WSAStartup(wVersion, &wsaData))
{
throw "WSAStartup fail!";
}
if (2 != HIBYTE(wsaData.wVersion) || 2 != LOBYTE(wsaData.wVersion))
{
throw "Verify version fail!";
}

    //创建一个SOCKET
    //socket
    SOCKET sock = ::socket(AF_INET, SOCK_STREAM, 0);
    if (INVALID_SOCKET == sock)
    {
        throw "Create socket fail!";
    }

    //初始化一个有效地址(给大堂经理一个胸签)
    //SOCKADDR_IN
    SOCKADDR_IN serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(12345);
    serverAddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.15");

    //连接服务器
    if (SOCKET_ERROR == ::connect(sock, (const sockaddr*)&serverAddr, sizeof(serverAddr)))
    {
        int iError = ::WSAGetLastError();
        cout << "connect fail!" << endl;
        //::Sleep(5000);
    }

    while (1)
    {

        int iSendData[2];
        cout << "Please input data:" << endl;
        cin >> iSendData[0] >> iSendData[1];
        ::send(sock, (const char*)&iSendData, sizeof(iSendData), 0);
        //::send(sock, "1111111", sizeof("1111111"), 0);
        //char szRecvBuf[100];
        //::recv(sock, (char*)szRecvBuf, sizeof(szRecvBuf), 0);
        //接收应答
        int iSum = 0;
        ::recv(sock, (char*)&iSum, sizeof(iSum), 0);
        cout << iSum <<endl;
    //  cout << szRecvBuf << endl;
    }


    ::closesocket(sock);

    ::WSACleanup();
}
catch (char* pStr)
{
    cout << pStr << endl;
    return -1;
}
return 0;

}


一运行客户端就显示连接失败,求大神解决


  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐