首页 > 代码库 > (8)Linux(客户端)和Windows(服务端)下socket通信实例
(8)Linux(客户端)和Windows(服务端)下socket通信实例
Linux(客户端)和Windows(服务端)下socket通信实例:
(1)首先是Windows做客户端,Linux做服务端的程序
Windows Client端
- #include <stdio.h>
- #include <Windows.h>
- #pragma comment(lib, "ws2_32.lib")
- #define Port 5000
- #define IP_ADDRESS "192.168.1.30" //服务器地址
- int main() // argc是命令行总的参数个数
- {
- WSADATA s; // 用来储存调用AfxSocketInit全局函数返回的Windows Sockets初始化信息
- SOCKET ClientSocket;
- struct sockaddr_in ClientAddr; // 一个sockaddr_in型的结构体对象
- int ret = 0;
- char SendBuffer[MAX_PATH]; // Windows的MAX_PATH默认是260
- // 初始化Windows Socket
- // WSAStartup函数对Winsock服务的初始化
- if (WSAStartup(MAKEWORD(2, 2), &s) != 0) // 通过连接两个给定的无符号参数,首个参数为低字节
- {
- printf("Init Windows Socket Failed! Error: %d\n", GetLastError());
- getchar();
- return -1;
- }
- while (1)
- {
- // 创建一个套接口
- // 如果这样一个套接口用connect()与一个指定端口连接
- // 则可用send()和recv()与该端口进行数据报的发送与接收
- // 当会话结束后,调用closesocket()
- ClientSocket = socket(AF_INET, // 只支持ARPA Internet地址格式
- SOCK_STREAM, // 新套接口的类型描述
- IPPROTO_TCP); // 套接口所用的协议
- if (ClientSocket == INVALID_SOCKET)
- {
- printf("Create Socket Failed! Error: %d\n", GetLastError());
- getchar();
- return -1;
- }
- ClientAddr.sin_family = AF_INET;
- ClientAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS); // 定义IP地址
- ClientAddr.sin_port = htons(Port); // 将主机的无符号短整形数转换成网络字节顺序
- memset(ClientAddr.sin_zero, 0X00, 8); // 函数通常为新申请的内存做初始化工作
- // 连接Socket
- ret = connect(ClientSocket,
- (struct sockaddr*)&ClientAddr,
- sizeof(ClientAddr));
- if (ret == SOCKET_ERROR)
- {
- printf("Socket Connect Failed! Error:%d\n", GetLastError());
- getchar();
- return -1;
- }
- else
- {
- printf("Socket Connect Succeed!");
- }
- printf("Input Data: ");
- while (1)
- {
- scanf("%s", &SendBuffer);
- // 发送数据至服务器
- ret = send(ClientSocket,
- SendBuffer,
- (int)strlen(SendBuffer), // 返回发送缓冲区数据长度
- 0);
- if (ret == SOCKET_ERROR)
- {
- printf("Send Information Failed! Error:%d\n", GetLastError());
- getchar();
- break;
- }
- break;
- }
- // 关闭socket
- closesocket(ClientSocket);
- if (SendBuffer[0] == ‘q‘) // 设定输入第一个字符为q时退出
- {
- printf("Quit!\n");
- break;
- }
- }
- WSACleanup();
- getchar();
- system("pause");
- return 0;
- }
Linux server端
- #include <stdio.h>
- #include <sys/socket.h>
- #include <sys/types.h> /* See NOTES */
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <string.h>
- #include<unistd.h>
- #include <cstdlib>
- #define SERVER_PORT 5000
- #define LENGTH_OF_LISTEN_QUEUE 20
- #define BUFFER_SIZE 10
- int main() // (int argc, char* argv[])
- {
- struct sockaddr_in server_addr;
- int server_socket;
- int opt = 1;
- bzero(&server_addr, sizeof(server_addr)); // 置字节字符串前n个字节为0,包括‘\0‘
- server_addr.sin_family = AF_INET;
- server_addr.sin_addr.s_addr = htons(INADDR_ANY); // 转小端,INADDR_ANY就是指定地址为0.0.0.0的地址
- server_addr.sin_port = htons(SERVER_PORT);
- // 创建一个Socket
- server_socket = socket(PF_INET, SOCK_STREAM, 0);
- if (server_socket < 0)
- {
- printf("Create Socket Failed!\n");
- exit(1);
- }
- // bind a socket
- setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
- if(bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)))
- {
- printf("Server Bind Port: %d Failed!\n", SERVER_PORT);
- exit(1);
- }
- // 监听Socket
- if (listen(server_socket, LENGTH_OF_LISTEN_QUEUE))
- {
- printf("Server Listen Failed!\n");
- exit(1);
- }
- while(1)
- {
- struct sockaddr_in client_addr;
- int client_socket;
- socklen_t length;
- char Buffer[BUFFER_SIZE];
- // 连接客户端Socket
- length = sizeof(client_addr);
- client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length);
- if (client_socket < 0)
- {
- printf("Server Accept Failed!\n");
- break;
- }
- // 从客户端接收数据
- while(1)
- {
- bzero(Buffer, BUFFER_SIZE);
- length = recv(client_socket, Buffer, BUFFER_SIZE, 0);
- if (length < 0)
- {
- printf("Server Recieve Data Failed!\n");
- break;
- }
- if (‘q‘ == Buffer[0])
- {
- printf("Quit!\n");
- break;
- }
- printf("%s\n", Buffer);
- break;
- }
- close(client_socket);
- }
- close(server_socket);
- return 0;
- }
(2)Windows做服务端,Linux做客户端实例
(8)Linux(客户端)和Windows(服务端)下socket通信实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。