Instance parsing SOCKET programming model (-)

Source: Internet
Author: User
Tags bind socket port number
Socket (Socket) is a protocol-independent network programming interface. In the OSI model, it focuses on the session layer and transmission layer. Socket actually represents a valid endpoint for communication between two entities. You can obtain the source IP address, source port, destination IP address, and destination port through socket. You can connect multiple sockets to the same port so that multiple connections can be established on a single port. Through socket client/server programming, you can create a distributed program that can be used by many people, and all customers can work with a uniform front-end and communicate with the server. This is very similar to the call process between old-fashioned or new-style telephones.
In. NET, the System. Net. Sockets namespace provides a managed implementation of the Windows Sockets (Winsock) interface for developers who need to strictly control network access. The Socket class provides a rich set of methods and attributes for network communication.
If the application only needs one thread during execution, use the following methods, which are applicable to synchronous operation mode.
If you are using a connection-oriented protocol (such as TCP), the server can use the Listen method to Listen for connections. The Accept method processes any incoming connection requests and returns the Socket that can be used to communicate with the remote host. You can use the returned Socket to call the Send or Receive method. To specify the local IP address and port number, call the Bind method before calling the Listen method. If Bind is not called, the basic service provider assigns these values to you. Then, you can use the LocalEndPoint attribute to identify the IP address and port number allocated to the Socket. To Connect to the listener host, call the Connect method. To perform data communication, call the Send or Receive method.
If you are using a non-connection protocol (such as UDP), you do not need to listen for connections. Call the ReceiveFrom method to accept any incoming datagram. The SendTo method can be used to send a datagram to a remote host.
Develop a simple synchronous network chat program, which can be divided into servers and clients. After the connection is successful, the server and the client can communicate with each other. The source code provides detailed comments on every class and method attribute that is difficult for beginners to understand. The following is the server code.
Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;
Using System. Net;
Using System. Net. Sockets;
Using System. Threading;
Using System. Text;
Namespace chat_socket
{
/// <Summary>
/// Summary of Form1.
/// </Summary>
Public class Form1: System. Windows. Forms. Form
{

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.