Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1300340
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: LINUX

2011-03-05 21:39:55

  1. 这是window的API程序与linux的API程序进行简单通讯的例子

  2. ========================================================

  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>

  7. #define SIZE 1024
  8. #define IPSIZE 16

  9. int main(void)
  10. {
  11.         int sd, nsd;
  12.         struct sockaddr_in addr, peer;
  13.         int len, ret;
  14.         char buffer[SIZE];
  15.         unsigned short port = 19790;
  16.         //unsigned short port = 1029;

  17.         char ipstr[IPSIZE];

  18.         sd = socket(PF_INET, SOCK_STREAM, 0);
  19.         if (sd == -1)
  20.         {
  21.                 perror("socket");
  22.                 return -1;
  23.         }

  24.         addr.sin_family = AF_INET;
  25.         addr.sin_port = htons(port);
  26.         inet_pton(AF_INET, "0.0.0.0", &addr.sin_addr);

  27.         bind(sd, (struct sockaddr *)&addr, sizeof(addr));

  28.         listen(sd, 10);

  29.         while(1)
  30.         {
  31.                 len = sizeof(peer);
  32.                 nsd = accept(sd, (struct sockaddr *)&peer, &len);

  33.                 printf("1");
  34.                 ret = recv(nsd, buffer, SIZE, 0);
  35.                 buffer[ret] = '\0';

  36.                 // inet_pton(AF_INET, &peer.sin_addr, ipstr, IPSIZE);

  37.                 printf("server read [%s:%d] : %s\n",
  38.                         inet_ntoa(peer.sin_addr), ntohs(peer.sin_port), buffer);

  39.                 close(nsd);
  40.         }
  41.         close(sd);

  42.         return 0;
  43. }

  44. ===============================================================

  45. /* 头文件 */
  46. #include <stdio.h>
  47. #include "winsock2.h"
  48. /* 常量 */
  49. #define RECV_BUFFER_SIZE 8192

  50. /*************************************
  51. * main
  52. * 功能 socket通信客户端
  53. **************************************/
  54. void main(int argc, char* argv[])
  55. {
  56.  // 变量定义

  57.  SOCKADDR_IN clientService;// 地址

  58.  SOCKET ConnectSocket;// socket

  59.  WSADATA wsaData;// 库

  60.  LPVOID recvbuf;// 接收缓存

  61.  int bytesSent;
  62.  int bytesRecv = 0;
  63.  char sendbuf[32] = "get information";// 默认发送的数据


  64.  // 初始化socket库, 保存ws2_32.dll已经加载

  65.  int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  66.  if (iResult != NO_ERROR)
  67.   printf("Error at WSAStartup()\n");

  68.  // 创建socket


  69.  ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP );
  70.  if (ConnectSocket == INVALID_SOCKET)
  71.  {
  72.   printf("Error at socket(): %ld\n", WSAGetLastError());
  73.   WSACleanup();
  74.   return;
  75.  }

  76.  // 设置服务端的通信协议、IP地址、端口

  77.  clientService.sin_family = AF_INET;
  78.  clientService.sin_addr.s_addr = inet_addr( "192.168.1.8" );
  79.  clientService.sin_port = htons( 19790);

  80.  // 连接到服务端

  81.  printf("befor connect\n");
  82.  if ( connect(ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR)
  83.  {
  84.   printf( "Failed to connect(%d)\n",WSAGetLastError() );
  85.   WSACleanup();
  86.   return;
  87.  }
  88.  printf("after connect\n");

  89.  // 向服务端发送数据

  90.  bytesSent = send( ConnectSocket, "Hello", lstrlen("Hello")+1, 0 );// 无标志


  91.  WSACleanup();
  92.  while(1);

  93.  return;
  94. }

  95. =====================================================================

  96. [附加网站]

  97.  http://blog.sina.com.cn/s/blog_500c9c600100bqnr.html


  98. http://wenku.baidu.com/view/4150c707e87101f69e319513.html


  99. http://apps.hi.baidu.com/share/detail/7267986


  100. http:
阅读(750) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~