自言自语ing 2015-08-02 03:04 采纳率: 0%
浏览 3642

linux虚拟机与windows间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条回答 默认 最新

  • oyljerry 2015-08-02 03:09
    关注

    客户端连接失败时的错误码是多少?是不是不能连接,防火墙等拦截请求了

    评论

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页