Experiment three, socket programming experiment __ Programming

Source: Internet
Author: User
Tags ack bind printf socket strlen port number server port htons

experiment three, socket programming experiment

First, the purpose of the experiment

1. Learn Sockets Programming basics and C-based socket programming related functions and data types

2. Implement a simple client/server program

3. Master the communication Principle of UDP, TCP client/server mode. Proficiency in socket programming commands

Second, the principle of experiment

implementation of a simple client/server program

It uses a socket interface to send messages on a TCP connection. This program also uses other UNIX network functions.

L allows users to enter and send resulting to the machine on the other end of the machine at one end. It is a simplified version of talk in Unix, similar to the core program of a web chat room.

Client

1. The client uses the remote machine name as the parameter. It calls the UNIX program gethostbyname to convert the name to the IP address of the remote host.

2. Next construct the address data structure (SIN) required for the socket interface. Note that this data structure indicates that we will always use sockets to connect to the Internet (AF_INET). In this example, we use the TCP port number 5432 as the server port number that is known to all, and it happens to be not the port number assigned to other Internet services.

3. The final step in establishing a connection is to call socket and connect. Once the connect operation is returned and the connection is established, the client program enters the main loop and continuously reads the text from the standard input and sends it through the socket.

Server

1. The server first fills in its own port number (Server_port) to construct the address data structure.

2. Second, it does not specify an IP address, which allows the application to accept connections from any local IP address.

3. The server then performs the initial steps related to passive opening: Establish a socket, bind it to a local address, and set the maximum number of simultaneous connections allowed.

4. Finally, the main loop waits for the remote host to attempt to connect, and when the remote has a host attempting to connect to it, it receives and outputs the characters sent on the connection.

Sockets Programming Basics

Network programming is through the computer network and other programs to communicate the program, socket programming is the mainstream of network programming tools.

The socket API appeared in the early the 1980s as the Berkeley Unix (BSD 4.2) operating system library to pass interprocess communication functions. The socket API is now available for mainstream operating systems.

In Unix-based systems, such as BSD and Linux systems, the socket API is part of the operating system kernel.

In MS-DOS, Windows OS, OS/2, and other operating systems, the socket API is provided as a library, such as in Windows systems, where the socket API is called Winsock. The Socket API is a programming facility for interprocess communication and a mechanism for providing the underlying abstraction between processes.

Although application developers rarely need to write code at that level, it is important to understand the socket API.

First, high-level facilities are built on the socket API, and they are implemented using the operations provided by the socket API.

Second, for applications with high response times or running on a limited resource platform, even the socket API is the only available interprocess communication facility.

Socket Interface Specification can be applied to a variety of communication protocols, mainly TCP/IP. TCP/IP is the most commonly used network communication protocol for computer interconnection, the core of TCP/IP is implemented by the kernel of the network operating system, and the application accesses TCP/IP through programming interface.

TCP/IP uses a network address and a service port number to uniquely identify the device .

L Network address identifies a specific device on the network

L port number identifies the specific service on the device to which you want to connect

the basic mode of network communication is as follows: Each host of communication has a unique IP address in this network environment, there are often many communication programs exist on a host, each of which consumes a communication port. Therefore, an IP address, a communication port, can determine the location of a communication program.

TCP Transmission Control Protocol (Transport-Protocol) is a connection-oriented, reliable transport-layer protocol. Connection-oriented means that a normal TCP transmission needs to be done by establishing a specific virtual circuit connection between the TCP client and the TCP server, which is often referred to as a " three-time handshake ."

Reliability can be guaranteed in a number of ways, where we are concerned with data sequences and confirmations .

1. TCP uses the serial number in data fragmentation (Segment) to ensure that all transmitted data can be reorganized at the remote end in the normal order, and that the integrity of the data transfer is guaranteed.

2. To transmit data over TCP, a connection must be established between the two hosts. For example, the TCP client needs to establish a connection to the TCP server.

In the first step, the client presents a connection request to the server. This is where the TCP SYN flag is placed. The client tells the server that the serial number area is legal and needs to be checked. The client inserts its own isn in the serial number area of the TCP header.

The second step, after the server receives the TCP segment, in the second step with its own isn response (SYN flag set), while acknowledging the receipt of the client's first TCP segment (ACK flag set).

In the third step, the client confirms receipt of the service side of the ISN (ACK flag set). This is the time to establish a full TCP connection and start the data transfer process in full duplex mode.

Iii. contents of the experiment

According to the above content to write a TCP Client/server mode communication program.

In fact, the network program is composed of two parts-the client and server side. They are set up in the following steps

Server-Side

socket-->bind-->listen-->accept

Client

Socket-->connect

The experiment is carried out as follows:

1. Write the UDP, TCP client/server mode communication program;

2. Debug and run your own written implementation program;

Understand the working principle of TCP Client/server mode, compare the two differences, such as abnormal situation, in the experimental report to write the cause analysis.

This procedure excerpt xinxin Sun "VC + + in-depth detailed"

UDP, TCP client/server mode communication program

TCP server: #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 ("222.25.177.246");
	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 was Weijian peng!", strlen ("This is weijianpeng!")

	+1,0);
	Closesocket (sockclient);
WSACleanup ();
	The TCP client #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 ("222.25.177.246");
	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 was Weijian peng!", strlen ("This is Weijian peng!")

	+1,0);
	Closesocket (sockclient);
WSACleanup ();
	}//udp server #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 client #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 ("222.25.177.246");
	Addrsrv.sin_family=af_inet;
	Addrsrv.sin_port=htons (6000); SendTo (sockclient, "hello,my name is pengweijian!", strlen ("Hello,my name is pengweijian!")
	+1,0, (sockaddr*) &addrsrv,sizeof (sockaddr));
	Closesocket (sockclient);
WSACleanup ();
 }


 

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.