windows客户端和linux服务器之间的通信? [英] communication between windows client and linux server?

查看:82
本文介绍了windows客户端和linux服务器之间的通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 c 中执行客户端(windows)和服务器(linux RT)之间的通信.我为 Windows 操作系统(一台笔记本电脑)编写了客户端代码,为 linux 操作系统(另一台笔记本电脑)编写了服务器代码.我通过以太网电缆连接两台笔记本电脑,并将它们配置在同一子网上.

I am performing communication between client(windows) and server(linux RT) in c. I have written a client code for windows operating system (one laptop) and server code for linux operating system (another laptop). I am connecting the both laptop via ethernet cable and configured them on the same subnet.

SERVER.c:Linux

SERVER.c : Linux

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 512
#define PORT 9930

void err(char *str)
{
    perror(str);
    exit(1);
}

int main(void)
{
    struct sockaddr_in my_addr, cli_addr;
    int sockfd, i;
    socklen_t slen=sizeof(cli_addr);
    char buf[BUFLEN];

    if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
      err("socket");
    else
      printf("Server : Socket() successful\n");

    bzero(&my_addr, sizeof(my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(PORT);
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(sockfd, (struct sockaddr* ) &my_addr, sizeof(my_addr))==-1)
      err("bind");
    else
      printf("Server : bind() successful\n");

    while(1)
    {
        if (recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&cli_addr, 

&slen)==-1)
            err("recvfrom()");
        printf("Received packet from %s:%d\nData: %s\n\n",
               inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), buf);
    }

    close(sockfd);
    return 0;
}

CLIENT.c - 窗口

CLIENT.c - windows

#pragma comment(lib, "Ws2_32.lib")

#include <windows.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <winsock.h>
#include <io.h> 
#define BUFLEN 512
#define PORT 9930

void err(char *str)
{
    perror(str);
    exit(1);
}

int main(void)
{
    struct sockaddr_in my_addr, cli_addr;
    int sockfd, i;
    socklen_t slen=sizeof(cli_addr);
    char buf[BUFLEN];
    WORD wVersionRequested;
    WSADATA wsaData;

     printf("Initializing Winsock\n");
     wVersionRequested = MAKEWORD (1, 1);
     if (WSAStartup (wVersionRequested, &wsaData) != 0){
      printf("Winsock initialised failed \n");
     } else {
       printf("Initialised\n");


if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    err("socket");

bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_aton(argv[1], &serv_addr.sin_addr)==0)
{
    fprintf(stderr, "inet_aton() failed\n");
    exit(1);
}


     while(1)
{
    printf("\nEnter data to send(Type exit and press enter to exit) : ");
    scanf("%[^\n]",buf);
    getchar();
    if(strcmp(buf,"exit") == 0)
      exit(0);

    if (sendto(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&serv_addr, slen)==-1)
        err("sendto()");
}

close(sockfd);
return 0;

}

我的问题:

  • 是否可以进行这样的交流??
  • 我要为此采取具体措施吗??

请给出关于此的想法?

推荐答案

您可以直接连接两个系统(通过以太网电缆),但通常您必须为此使用特殊电缆:它被称为交叉电缆".否则无法连接.

You can connect two systems directly (via ethernet cable), but typically you must use a special cable for that: it is called a "crossover cable". Otherwise no connection is possible.

较新的网络控制器实现了对此类设置的检测,因此可能可以为此设置使用标准电缆,但这取决于两个系统中内置的网络控制器.您将不得不尝试或查阅文档.

Newer network controllers implement a detection for this kind of setup, so it might be possible to use a standard cable for this setup, but this depends on the network controllers build into the two systems. You will have to try or consult the documentation.

也可能是您必须在 MS-Windows 端(在网络适配器配置内)选择一些特殊配置才能使其工作.我几次遇到标准设置的通信问题.你可以向谷歌咨询这些设置.

Also it migh be that you have to select some special configuration on the MS-Windows side (inside the network adapter configuration) for this to work. I experienced communication problems with the standard setup a few times. You can consult google for those settings.

这篇关于windows客户端和linux服务器之间的通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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