VC + + Socket Communication Example Summary __c++

Source: Internet
Author: User
Tags set socket strlen htons

1. Two-day computer communications need to be agreed, the two computer IP must be the only

2. The same computer can carry out multiple applications to communicate with other computers with unique IP addresses, and the port number is the only indication that distinguishes the same computer (the same IP).

3. is actually similar to an Office switchboard number (IP) and extension number (port number)

4. Agreement: Rules, standards, or conventions established for the purpose of data exchange (communication) in a network

5. Protocol = semantics + syntax + rules

6. Different layers have their own different protocols

7. The upper level uses the service provided by the lower level, the actual communication completes at the lowest level

8. Virtual communication between peer-level entities |

9. TCP: Secure connection-oriented transport protocol

UDP: Non-connected unreliable transport protocols

11. A port is an abstract software structure. After an application is connected to a port through a system call, the data sent to the port by the transport layer is received by the response, and the data from the corresponding process to the transport layer is exported through that port.

12. The port is represented by an integer identifier. The port number is related to the Protocol, two protocols TCP and UDP for the TCP/IP transport layer are completely independent of two software modules, so the respective port numbers are also independent of each other.

13. The port uses a 16-digit number to indicate that the range is 0~65535,1024 the following port number to the predefined service. For example, HTTP uses port 80.

Socket (SOCKET): Windows Sockets only supports one communication area: the internetwork (AF_INET), which is used by processes using the Internetwork protocol cluster.

C/S mode: The client requests the server, the server receives the request, provides the corresponding service.

16. Type of socket

A) Streaming sockets: (SOCK_STREAM): Connection-oriented reliable data transmission services, in order to receive

b Data Packet sockets: (SOCK_DGRAM): connectionless service. The reception order is confusing.

c) original socket (Sock_ram)

17. TCP (Connection-oriented)-based socket programming

A) server-side programs:

I. Create socket socket

Ii. bind a socket to a local address and port bind

Iii. set socket to listener mode, prepare to receive customer request listen

IV. Wait for the customer's request to arrive; When the request arrives, receive the connection request and return a new socket for this connection accept

V. Communication with the returned socket and client REND/RECV

Vi. return, waiting for another client request

Vii. closing sockets

b) Client programs:

I. Create socket socket

Ii. sending a connection request to the service side Connect

III. communication with the server side REND/RECV

Iv. closing sockets

18. Socket programming based on UDP

A server side (receiving) program:

I. Create socket socket

Ii. bind a socket to a local address and port bind

Iii. waiting to receive data recv/from

Iv. closing sockets

b Client (sending) program:

I. Create socket socket

Ii. sending data to the server SendTo

Iii. closing sockets

int WSAStartup (

WORD wversionrequested
lpwsadata lpwsadata//is a structural body that receives the details of the socket
);

Alt+f8: Formatting code

21. TCP-oriented connection, server-side

A) sockets

Socket Socket (
int af,//Specify address cluster
int type,//socket type
int Protocol//protocol

);

b) Binding

int bind (
 Socket S,//socket to bind
Name,//specified local address information for this socket
int Namelen//length of the address structure
);

struct SOCKADDR {

U_short sa_family;

Char sa_data[14];

};

c) Monitoring

int Listen (
SOCKET S
Int
);

D) Accept Connections

SOCKET Accept (
SOCKET S
struct sockaddr far* addr
int far*
);
e) Send
int Send (
socket s,//Set up a connected socket
const Char FAR * buf,//Sent data
int len,//length of data sent
Int
);
Receive
int recv (
SOCKET S
Char far* buf
int len
Int
);
g) Required Documents
#include <winsock2.h>
Need to connect a dynamic link library: Ws2_32.lib

22. Server-side:

A) Socket

b) Connection: Connect

int Connect (
SOCKET s,
Name
Int
);

c) Receive: Recv

d) Sent: send

e) off: closesocket

TCP Server-side programs:

#include <winsock2.
	h> #include <stdio.h> void Main () {WORD wversionrequested;
	Wsadata Wsadata;
	
	int err;
	
	wversionrequested = Makeword (1, 1);
	Err = WSAStartup (wversionrequested, &wsadata);
	if (Err!= 0) {return;
        } if (Lobyte (wsadata.wversion)!= 1 | |
		Hibyte (wsadata.wversion)!= 1) {wsacleanup (); 
	Return

	SOCKET Socksrv=socket (af_inet,sock_stream,0);
	Sockaddr_in addrsrv; Addrsrv.sin_addr. S_un.
	S_addr=htonl (Inaddr_any);
	Addrsrv.sin_family=af_inet;

	Addrsrv.sin_port=htons (6000);

	Bind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr));

	Listen (socksrv,5);
	Sockaddr_in addrclient;

	int len=sizeof (SOCKADDR);
		while (1) {SOCKET sockconn=accept (socksrv, (sockaddr*) &addrclient,&len);
		Char sendbuf[100];
		sprintf (SendBuf, "Welcome%s to Lau wa Village", Inet_ntoa (ADDRCLIENT.SIN_ADDR));
		Send (Sockconn,sendbuf,strlen (SENDBUF) +1,0);
		Char recvbuf[100];
		Recv (sockconn,recvbuf,100,0);
		printf ("%s\n", recvbuf); Closesocket (SOCKCONN); }

}

TCP client program:

#include <winsock2. h>
#include <stdio.h>
void Main ()
{
	WORD wversionrequested;
	Wsadata Wsadata;
	int err;
	
	wversionrequested = Makeword (1, 1);
	
	Err = WSAStartup (wversionrequested, &wsadata);
	if (Err!= 0) {return
		;
	}
	
	if (Lobyte (wsadata.wversion)!= 1 | |
        Hibyte (wsadata.wversion)!= 1) {
		wsacleanup ();
		return; 
	}
	SOCKET Sockclient=socket (af_inet,sock_stream,0);

	Sockaddr_in addrsrv;
	Addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");//server-side address
	addrsrv.sin_family=af_inet;
	Addrsrv.sin_port=htons (6000);
	Connect (sockclient, (sockaddr*) &addrsrv,sizeof (sockaddr));

	Char recvbuf[100];
	Recv (sockclient,recvbuf,100,0);
	printf ("%s\n", recvbuf);
	Send (Sockclient, "This is Lau wa cun", strlen ("This is Lau wa Village") +1,0);

	Closesocket (sockclient);
	WSACleanup ();
}

25. After the operation, opened the 6 client, a server side. Program Run Effect:

26. UDP-oriented connections

27. Server: Socket

A) Bind

b) Receive data:

int Recvfrom (
socket s,//Socket
Char far* buf,//receiving data
int len,//Length
int Flags,//will affect the invocation behavior, 0
struct sockaddr far* from,//Receive sender's address information
int far* Fromlen//Receive length
);

c) Close Closesocket

D) Cleanup

UDP server-side programs:

#include <winsock2. h>
#include <stdio.h>

void Main ()
{
	WORD wversionrequested;
	Wsadata Wsadata;
	int err;
	
	wversionrequested = Makeword (1, 1);
	
	Err = WSAStartup (wversionrequested, &wsadata);
	if (Err!= 0) {return
		;
	}
	
	if (Lobyte (wsadata.wversion)!= 1 | |
        Hibyte (wsadata.wversion)!= 1) {
		wsacleanup ();
		return; 
	}

	SOCKET Socksrv=socket (af_inet,sock_dgram,0);
	Sockaddr_in addrsrv;
	Addrsrv.sin_addr. S_un. S_addr=htonl (inaddr_any);
	addrsrv.sin_family=af_inet;
	Addrsrv.sin_port=htons (6000);

	Bind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr));

	Sockaddr_in addrclient;
	int len=sizeof (SOCKADDR);
	Char recvbuf[100];

	Recvfrom (socksrv,recvbuf,100,0, (sockaddr*) &addrclient,&len);
	printf ("%s\n", recvbuf);
	Closesocket (SOCKSRV);
	WSACleanup ();

}

UDP clients:

A) socket definition

b) Send:

int SendTo (
SOCKET S
const Char FAR * buf
int len
int Flags
const struct SOCKADDR FAR * to
Int
);

UDP client Programs

#include <winsock2. h>
#include <stdio.h>

void Main ()
{
	WORD wversionrequested;
	Wsadata Wsadata;
	int err;
	
	wversionrequested = Makeword (1, 1);
	
	Err = WSAStartup (wversionrequested, &wsadata);
	if (Err!= 0) {return
		;
	}
	
	if (Lobyte (wsadata.wversion)!= 1 | |
        Hibyte (wsadata.wversion)!= 1) {
		wsacleanup ();
		return; 
	}
	
	SOCKET Sockclient=socket (af_inet,sock_dgram,0);
	Sockaddr_in addrsrv;
	Addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");
	addrsrv.sin_family=af_inet;
	Addrsrv.sin_port=htons (6000);

	SendTo (sockclient, "Hello Liu wa Cun", strlen ("Hello Liu wa cun") +1,0,
		(sockaddr*) &addrsrv,sizeof (sockaddr));
	Closesocket (sockclient);
	WSACleanup ();
}
Run Result:

30. Note: Each program should add a ws2_32.lib link library. Add as follows:



                                      by   Liu Wa village

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.