send.c放在主机上

recv.c放在虚拟机上

虚拟机的设置,选择birdge连接方式


 

编译环境是vc6.0

测试软件是wireshark

源代码

send.c //发送

#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "wsock32.lib")

typedef int   int32;
typedef short int16;
typedef char  int8;

typedef unsigned int   uint32;
typedef unsigned short uint16;
typedef unsigned char  uint8;

typedef  unsigned char boolean;

SOCKET listen_socket;     /* socket of waiting receiving data */

/* binded address */
static struct sockaddr_in srv;

/* destination address of sending(send CLIENTS_NUM ports at one time) */
static struct sockaddr_in dest;

/* port to listen on */
uint16 listening_port;

//#define ERR_PRINT(fmt,...)

int main(void)
{
  char snd_buf[100];
  int num;
  WSADATA  Ws;
  //Init Windows Socket
  //必须有这个WSAStartup函数,不然socket不成功
  if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
  {
    printf("Init Windows Socket Failed::%d",GetLastError());
    return -1;
  }

  listen_socket = socket(PF_INET, SOCK_DGRAM, 0);

  if ( listen_socket == INVALID_SOCKET )
  {
    printf("Error: create socket failed %d/n",listen_socket);
    return FALSE;
  }
   
  srv.sin_family = AF_INET;
  //srv.sin_addr.s_addr = htonl( INADDR_ANY );  /* ANY Address 本机*/
  srv.sin_addr.s_addr=inet_addr("192.168.1.121");//虚拟机的IP
  srv.sin_port = htons(6000);
//这里不需要绑定,不然绑定不成功,返回错误码10049
/*  if (bind( listen_socket, (struct sockaddr *)&srv, sizeof(srv)) != 0)
  {
    printf("Error: bind failed. Error code: %d/n", GetLastError());
    closesocket( listen_socket );
    return FALSE;
  }
*/
  memset(snd_buf,0,100);
  sprintf(snd_buf,"aaaaaaaaaaaaaaamessage from client/n");
    printf("send buf %s/n",snd_buf);
  num = sendto(listen_socket,snd_buf,sizeof(snd_buf),0,(struct sockaddr*)&srv,sizeof(struct sockaddr));
  if (num == SOCKET_ERROR) {
        printf("Send Info Error %d",GetLastError());
  }
  printf("send %d characters/n",num);
  closesocket(listen_socket);

  return 0;
}

 

 

 

 

recv.c  //接收

 

#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "wsock32.lib")

typedef int   int32;
typedef short int16;
typedef char  int8;

typedef unsigned int   uint32;
typedef unsigned short uint16;
typedef unsigned char  uint8;

typedef  unsigned char boolean;

SOCKET listen_socket1;     /* socket of waiting receiving data */

SOCKET listen_socket;     /* socket of waiting receiving data */


/* binded address */
static struct sockaddr_in srv;

/* destination address of sending(send CLIENTS_NUM ports at one time) */
static struct sockaddr_in clt;

/* port to listen on */
uint16 listening_port;
int32 len;
//#define ERR_PRINT(fmt,...)

int main(void)
{
  char recv[100];
  int num;
  WSADATA  Ws;

  //Init Windows Socket
  if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 )
  {
    printf("Init Windows Socket Failed::",GetLastError());
    return -1;
  }

  listen_socket1 = socket(PF_INET, SOCK_DGRAM, 0);

  if ( listen_socket1 == INVALID_SOCKET )
  {
    printf("Error: create socket failed/n");
    return FALSE;
  }
   
  srv.sin_family = PF_INET;
  srv.sin_addr.s_addr = htonl( INADDR_ANY );  /* ANY Address */
  srv.sin_port = htons(6000);

  if (bind( listen_socket1, (struct sockaddr *)&srv, sizeof(srv)) != 0)
  {
    printf("Error: bind failed. Error code: %d/n", GetLastError());
    closesocket( listen_socket1 );
    return FALSE;
  }
  len = sizeof(struct sockaddr_in);
  memset(recv,0,100);
  //sprintf(recv,"aaaaaaaaaaaaaaamessage from client/n");

  num = recvfrom(listen_socket1, recv, sizeof(recv), 0, (struct sockaddr*)&recv, &len);
  printf("receive %d characters/n",num);
  closesocket(listen_socket1);

  return 0;
}

 

 

先执行虚拟机里面的recv.c

弹出对话框,阻塞等待数据的到来

然后执行主机下的send.c程序

显示数据发送完毕

 

 

再看虚拟机里的执行窗口

 

 

wireshark显示

 

 

 

wireshark设置过滤条件

虚拟机

ip.addr==192.168.1.121

主机

ip.addr==192.168.1.121

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐