windows 与 Linux SOCKET通讯
时间:2014-06-03 12:07:25
收藏:0
阅读:400
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 |
windows client 端口 // Def_win_client_socket_test.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <windows.h> #pragma comment(lib,"ws2_32.lib") #define PORT 4000 #define IP_ADDRESS "192.168.227.136" int
_tmain( int
argc, _TCHAR* argv[]) { WSADATA Ws; SOCKET ClientSocket; struct
sockaddr_in ClientAddr; int
Ret = 0; char
SendBuffer[MAX_PATH]; if (WSAStartup(MAKEWORD(2,2),&Ws) != 0) { printf( "Init winows socket failed::%d" ,GetLastError()); return
-1; } // 0 表示系统自己选择协议 ClientSocket = socket(AF_INET,SOCK_STREAM,0); ClientAddr.sin_family = AF_INET; ClientAddr.sin_addr.S_un.S_addr=inet_addr(IP_ADDRESS); ClientAddr.sin_port=htons(PORT); printf( "Connect to server...\n" ); Ret = connect(ClientSocket,( struct
sockaddr*)&ClientAddr, sizeof (ClientAddr)); if ( Ret == SOCKET_ERROR) { printf( "Connect Error::%d\n" ,GetLastError()); return
-1; } else { printf( "Connect succeded! \n" ); } while (1) { scanf( "%s" , SendBuffer); /* send data to server */ Ret = send(ClientSocket, SendBuffer, ( int )strlen(SendBuffer), 0); if ( Ret == SOCKET_ERROR ) { printf( "Send Info Error::%d\n" , GetLastError()); break ; } if ( ‘q‘
== SendBuffer[0]) { break ; } } closesocket(ClientSocket); WSACleanup(); return
0; } |
编写的一个小程序 参考了网络上的一些文章,但是网络上的代码似乎设置于本机不同,在windows客户端这边做了些修改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 |
linux server 端口 #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define HELLO_WORLD_SERVER_PORT 4000 #define LENGTH_OF_LISTEN_QUEUE 20 #define BUFFER_SIZE 1024 int
main( int
argc, char
**argv) { struct
sockaddr_in server_addr; int
server_socket; int
opt = 1; bzero(&server_addr, sizeof (server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htons(INADDR_ANY); server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT); /* create a socket */ server_socket = socket(PF_INET,SOCK_STREAM,0); if ( server_socket < 0) { printf( "Create Socket Failed!" ); exit(1); } /* bind socket to a specified address*/ 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!" , HELLO_WORLD_SERVER_PORT); exit(1); } /* listen a socket */ if (listen(server_socket, LENGTH_OF_LISTEN_QUEUE)) { printf( "Server Listen Failed!" ); exit(1); } /* run server */ while
(1) { struct
sockaddr_in client_addr; int
client_socket; socklen_t length; char
buffer[BUFFER_SIZE]; /* accept socket from client */ length = sizeof (client_addr); client_socket = accept(server_socket, ( struct
sockaddr*)&client_addr, &length); if ( client_socket < 0) { printf( "Server Accept Failed!\n" ); break ; } /* receive data from client */ while (1) { bzero(buffer, BUFFER_SIZE); length = recv(client_socket, buffer, BUFFER_SIZE, 0); if (length < 0) { printf( "Server Recieve Data Failed!\n" ); break ; } else
if (length < 0) { printf( "Recv Buffer is %s\n" ,buffer); break ; } if ( ‘q‘
== buffer[0]) { printf( "Quit from client!\n" ); break ; } printf( "%s\n" , buffer); } close(client_socket); } close(server_socket); return
0; } |
ubuntu 虚拟机12.06版本 及 windows 7 下测试通过
评论(0)