首页 > 代码库 > (8)Linux(客户端)和Windows(服务端)下socket通信实例

(8)Linux(客户端)和Windows(服务端)下socket通信实例

Linux(客户端)和Windows(服务端)下socket通信实例:

(1)首先是Windows做客户端,Linux做服务端的程序

Windows   Client端

  1. #include <stdio.h>
  2. #include <Windows.h>
  3. #pragma comment(lib, "ws2_32.lib")
  4. #define Port 5000
  5. #define IP_ADDRESS "192.168.1.30"     //服务器地址
  6. int main() // argc是命令行总的参数个数
  7. {
  8. WSADATA s; // 用来储存调用AfxSocketInit全局函数返回的Windows Sockets初始化信息
  9. SOCKET ClientSocket;
  10. struct sockaddr_in ClientAddr; // 一个sockaddr_in型的结构体对象
  11. int ret = 0;
  12. char SendBuffer[MAX_PATH]; // Windows的MAX_PATH默认是260
  13. // 初始化Windows Socket
  14. // WSAStartup函数对Winsock服务的初始化
  15. if (WSAStartup(MAKEWORD(2, 2), &s) != 0) // 通过连接两个给定的无符号参数,首个参数为低字节
  16. {
  17. printf("Init Windows Socket Failed! Error: %d\n", GetLastError());
  18. getchar();
  19. return -1;
  20. }
  21. while (1)
  22. {
  23. // 创建一个套接口
  24. // 如果这样一个套接口用connect()与一个指定端口连接
  25. // 则可用send()和recv()与该端口进行数据报的发送与接收
  26. // 当会话结束后,调用closesocket()
  27. ClientSocket = socket(AF_INET, // 只支持ARPA Internet地址格式
  28. SOCK_STREAM, // 新套接口的类型描述
  29. IPPROTO_TCP); // 套接口所用的协议
  30. if (ClientSocket == INVALID_SOCKET)
  31. {
  32. printf("Create Socket Failed! Error: %d\n", GetLastError());
  33. getchar();
  34. return -1;
  35. }
  36. ClientAddr.sin_family = AF_INET;
  37. ClientAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS); // 定义IP地址
  38. ClientAddr.sin_port = htons(Port); // 将主机的无符号短整形数转换成网络字节顺序
  39. memset(ClientAddr.sin_zero, 0X00, 8); // 函数通常为新申请的内存做初始化工作
  40. // 连接Socket
  41. ret = connect(ClientSocket,
  42. (struct sockaddr*)&ClientAddr,
  43. sizeof(ClientAddr));
  44. if (ret == SOCKET_ERROR)
  45. {
  46. printf("Socket Connect Failed! Error:%d\n", GetLastError());
  47. getchar();
  48. return -1;
  49. }
  50. else
  51. {
  52. printf("Socket Connect Succeed!");
  53. }
  54. printf("Input Data: ");
  55. while (1)
  56. {
  57. scanf("%s", &SendBuffer);
  58. // 发送数据至服务器
  59. ret = send(ClientSocket,
  60. SendBuffer,
  61. (int)strlen(SendBuffer), // 返回发送缓冲区数据长度
  62. 0);
  63. if (ret == SOCKET_ERROR)
  64. {
  65. printf("Send Information Failed! Error:%d\n", GetLastError());
  66. getchar();
  67. break;
  68. }
  69. break;
  70. }
  71. // 关闭socket
  72. closesocket(ClientSocket);
  73. if (SendBuffer[0] == ‘q‘) // 设定输入第一个字符为q时退出
  74. {
  75. printf("Quit!\n");
  76. break;
  77. }
  78. }
  79. WSACleanup();
  80. getchar();
  81. system("pause");
  82. return 0;
  83. }

Linux server端

  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <sys/types.h> /* See NOTES */
  4. #include <sys/socket.h>
  5. #include <arpa/inet.h>
  6. #include <string.h>
  7. #include<unistd.h>
  8. #include <cstdlib>
  9. #define SERVER_PORT 5000
  10. #define LENGTH_OF_LISTEN_QUEUE 20
  11. #define BUFFER_SIZE 10
  12. int main() // (int argc, char* argv[])
  13. {
  14. struct sockaddr_in server_addr;
  15. int server_socket;
  16. int opt = 1;
  17. bzero(&server_addr, sizeof(server_addr)); // 置字节字符串前n个字节为0,包括‘\0‘
  18. server_addr.sin_family = AF_INET;
  19. server_addr.sin_addr.s_addr = htons(INADDR_ANY); // 转小端,INADDR_ANY就是指定地址为0.0.0.0的地址
  20. server_addr.sin_port = htons(SERVER_PORT);
  21. // 创建一个Socket
  22. server_socket = socket(PF_INET, SOCK_STREAM, 0);
  23. if (server_socket < 0)
  24. {
  25. printf("Create Socket Failed!\n");
  26. exit(1);
  27. }
  28. // bind a socket
  29. setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  30. if(bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)))
  31. {
  32. printf("Server Bind Port: %d Failed!\n", SERVER_PORT);
  33. exit(1);
  34. }
  35. // 监听Socket
  36. if (listen(server_socket, LENGTH_OF_LISTEN_QUEUE))
  37. {
  38. printf("Server Listen Failed!\n");
  39. exit(1);
  40. }
  41. while(1)
  42. {
  43. struct sockaddr_in client_addr;
  44. int client_socket;
  45. socklen_t length;
  46. char Buffer[BUFFER_SIZE];
  47. // 连接客户端Socket
  48. length = sizeof(client_addr);
  49. client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length);
  50. if (client_socket < 0)
  51. {
  52. printf("Server Accept Failed!\n");
  53. break;
  54. }
  55. // 从客户端接收数据
  56. while(1)
  57. {
  58. bzero(Buffer, BUFFER_SIZE);
  59. length = recv(client_socket, Buffer, BUFFER_SIZE, 0);
  60. if (length < 0)
  61. {
  62. printf("Server Recieve Data Failed!\n");
  63. break;
  64. }
  65. if (‘q‘ == Buffer[0])
  66. {
  67. printf("Quit!\n");
  68. break;
  69. }
  70. printf("%s\n", Buffer);
  71. break;
  72. }
  73. close(client_socket);
  74. }
  75. close(server_socket);
  76. return 0;
  77. }

(2)Windows做服务端,Linux做客户端实例

 

(8)Linux(客户端)和Windows(服务端)下socket通信实例