I. c ++ Server
(Visual Studio 2008 is used (or 03 should be fine, and VC ++ may need to be slightly changed ))
CodeAs follows:
# Include <winsock2.h>
# Include <stdio. h>
# Include <windows. h>
# Include <iostream>
Using namespace STD;
# Pragma comment (Lib, "ws2_32.lib ")
Void main (){
Word wversionrequested;
Wsadata;
Int err;
Short Port = 1300; // port number
Wversionrequested = makeword (1, 1 );
Err = wsastartup (wversionrequested, & wsadata); // initialize the socket
If (Err! = 0)
{
Return;
}
If (lobyte (wsadata. wversion )! = 1 | hibyte (wsadata. wversion )! = 1)
{
Wsacleanup ();
Return;
}
Socket socksrv = socket (af_inet, sock_stream, 0); // create a socket
Socket sockconn; // The socket used to communicate with the client.
Sockaddr_in addrsrv; // The socket address used to communicate with the client.
Addrsrv. sin_addr.s_un.s_addr = htonl (inaddr_any );
Addrsrv. sin_family = af_inet;
Addrsrv. sin_port = htons (port );
BIND (socksrv, (sockaddr *) & addrsrv, sizeof (sockaddr); // bind the port
Listen (socksrv, 5); // listen
Printf ("server % d is listening... \ n", Port );
Sockaddr_in addrclient;
Int Len = sizeof (sockaddr );
Char Buf [4096]; // received data
Char rbuf [100] = "successful"; // returned data
While (1)
{
// Accept the connection
Sockconn = accept (socksrv, (sockaddr *) & addrclient, & Len );
Printf ("Accept connection from % s \ n", inet_ntoa (addrclient. sin_addr ));
// Receive data
Int bytes;
If (Bytes = Recv (sockconn, Buf, sizeof (BUF), 0) = socket_error ){
Printf ("failed to receive data! \ N ");
Exit (-1 );
}
Buf [bytes] = '\ 0 ';
Printf ("Message from % s: % s \ n", inet_ntoa (addrclient. sin_addr), Buf );
// Send data
If (send (sockconn, rbuf, strlen (rbuf) + 1, 0) = socket_error ){
Printf ("failed to send data! ");
Exit (-1 );
}
Printf ("message to % s: % s \ n", inet_ntoa (addrclient. sin_addr), rbuf );
// Clear the resources occupied by the socket
Closesocket (sockconn );
}
}
The running effect is as follows:
Ii. Flex client (Flex builder)
The Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Export wedapplication xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute"
Creationcomplete = "initapp ();">
<Mx: SCRIPT>
<! [CDATA [
Import flash.net. Socket;
Private var socket: socket;
Private function initapp (): void
{
}
Private function starttest (): void
{
Socket = new socket ("127.0.0.1", 1300 );
Socket. addeventlistener (event. Connect, connecthandler );
Socket. addeventlistener (progressevent. socket_data, socketdatahandler );
}
Private function closetest (): void
{
If (socket! = NULL & socket. Connected)
{
Trace ("socket. Connected:" + socket. Connected );
Socket. Close ();
Trace ("socket. Connected:" + socket. Connected );
}
}
Private function connecthandler (Event: Event): void
{
Trace ("connecthandler:" + Event );
Socket. writemultibyte (sendstr. Text, "gb2312 ");
Socket. Flush ();
}
Private function socketdatahandler (Event: progressevent): void
{
Trace ("socketdatahandler:" + Event );
VaR STR: String = socket. readmultibyte (socket. bytesavailable, "gb2312 ");
Receivestr. Text = STR;
}
]>
</MX: SCRIPT>
<Mx: Text text = "send" Y = "11" x = "18" fontweight = "bold" color = "# ffffff"/>
<Mx: textareafont-size: 10pt; color: #990000; font-family: 'courier new'; MSO-font-kerning: 0pt "> receivestr" x = "53" Y = "68"/>
<Mx: Text text = "" Y = "69" x = "18" fontweight = "bold" color = "# ffffff"/>
<Mx: textareafont-size: 10pt; color: #990000; font-family: 'courier new'; MSO-font-kerning: 0pt "> sendstr" Y = "10" x = "53"/>
<Mx: button label = "start" Click = "starttest ()" x = "53" Y = "120"/>
<Mx: button label = "close" Click = "closetest ()" x = "126" Y = "120"/>
</MX: Using wedapplication>
Iii. Running
Run the C ++ server first and then the flex client, as shown in figure
1. socket based on TCP (connection oriented)
1. Server Side
- Create socket -------- Socket socket (int af, int type, int Protocol );
-
- Bind the socket to the local address and port ------ int BIND (_ in socket S, _ in const struct sockaddr * Name, _ in int namelen );
-
- Set the socket to the listening mode and prepare to accept the request -------- int listen (_ in socket S, _ in int backlog );
-
- Wait for the client request. After accepting the connection request, a new socket corresponding to the connection ------ socket accept (_ in socketS, _ Out struct sockaddr * ADDR, _ in_out int * addrlen );
- Use the socket returned by the accept function (which contains the IP address and port number of the client) for communication ----- send information int send (
Socket s,
Const char far * Buf,
Int Len,
Int flags
); ------------------ Accept information int Recv (socket S, char far * Buf, int Len, int flags );
-
- Wait for another client request
-
- Disable socket
2. Client
- Create socket ----- socket
- Send a connection request to the server ----- int connect (socketS, Const struct sockaddr far *Name, IntNamelen);
- Communication ---- send and Recv
- Disable socket
Ii. UPD-based socket
1. Server Side
-
- Create socket ----- socket
- Bind Address and port ------ bind
- Data processing ------ receive data int recvfrom (
_ In socket S ,
_ Out char * Buf ,
_ In int Len ,
_ In int Flags ,
_ Out struct sockaddr * From ,
_ In_out int * Fromlen
); -------------- Send data int sendto (
_ In socket S ,
_ In const char * Buf ,
_ In int Len ,
_ In int Flags ,
_ In const struct sockaddr * To ,
_ In int Tolen
);
-
- Disable socket
Client
- Create socket ----- socket
- Send data to the server and receive data recvfrom/sendto
- Disable socket
Do not forget to import the socket Library at the beginning ------ int wsastartup (
_ In WordWversionrequested,
_ Out lpwsadataLpwsadata
);
And introduce the header file # include <winsock2.h>