Document directory
- Technical Points
- Work with me
Use SOCKET to send and receive messages
In ASP. NET applications, messages can be sent and received through socket. Messages can be text content, files, or other types of data. This example describes how to use the method in the system. net. Sockets namespace to send and receive files.
Technical Points
This example describes how to use ASP. NET to send and receive files using methods in the system. net. Sockets namespace. In the sending process, we first introduce the method of reading files, read the files into the binary array, and then use the send () method in sockets to send the file data.
Work with me
1. Create a new ASP. NET application
Create an ASP. NET web application in the integrated development environment of Visual Studio. NET 2003 and name it example_12_7.
2. design page mysocket. aspx
Rename the default webform1.aspx page of the application example_12_7 to mysocket. aspx and add it on this page. The design page of mysocket. aspx is 12-19.
Figure 12-19 mysocket. aspx design page
The HTML Design Code of mysocket. aspx on the page is as follows:
<% @ Page Language = "C #" codebehind = "mysocket. aspx. cs" autoeventwireup = "false" inherits = "example_12_7.mysocket" %>
<HTML>
<Head>
<Title> example_12_7: Use SOCKET to send and receive messages </title>
</Head>
<Asp: Label id = "labelmessage" runat = "server"> the webpage is sending data. Please wait ...... </ASP: Label>
<Asp: button id = "receivebtn" runat = "server" text = "receive data"> </ASP: button>
<Asp: Label id = "labeldata" runat = "server"> </ASP: Label> </TD>
</Html>
3. Add a socket namespace
Because the page mysocket. when sending and receiving information, aspx needs to use the socket class to implement HTTP and other attributes. They are included in the namespace system. net and system. net. in sockets, You need to introduce these namespaces. The program code is as follows:
// Introduce the namespace
Using system. net;
Using system. IO;
Using system. net. Sockets;
4.create the sent file myfile.txt
Create the file myfile.txt sent by socketin the root directory of the application example_12_7. it is a text file of the text type, and its content is 12-20.
Figure 12-20 The myfile.txt file to be sent
5. design the mysocket. aspx events and functions on the page.
The page mysocket. aspx calls the page_load (Object sender, system. eventargs e) function to initialize. This function calls the socketsend () function to send file information. Socketsend( create the socketobject of the sent information first, define the IP address and port of the sent information, then read the information of the myfile.txt file, and finally call the function send () to send the data of the file. The program code for the functions page_load (Object sender, system. eventargs E) and socketsend () is as follows:
Private void page_load (Object sender, system. eventargs E)
{
If (! Page. ispostback)
{
// Send data
Socketsend ();
}
}
Private void socketsend ()
{
// Create a socket for sending data
Socket sendsocket = new socket (addressfamily. InterNetwork,
Sockettype. Stream, protocoltype. TCP );
// Set the data sending Address
Ipendpoint endpoint = new ipendpoint (IPaddress. parse ("127.0.0.1"), 80 );
// Create a stream for reading files
Filestream filesteam = new filestream (server. mappath ("myfile.txt "),
Filemode. openorcreate, fileaccess. Read );
// File Size
Byte [] fssize = new byte [filesteam. Length-1];
// Read the binary stream of the object
Binaryreader reader = new binaryreader (filesteam );
// Read data
Reader. Read (fssize, 0, (INT) filesteam. Length-1 );
// Link destination
Sendsocket. Connect (endpoint );
// Send data
Sendsocket. Send (fssize );
// Close the file stream
Filesteam. Close ();
// Close the data sending socket
Sendsocket. Shutdown (socketshutdown. Send );
Sendsocket. Close ();
}
Click mysocket. in aspx, the "receive data" button triggers the event receivebtn_click (Object sender, system. eventargs E), which calls the sendreceivedata () function to receive data sent from socketsend. The sendreceivedata () function first creates a socket object for receiving data, designs the IP address and port for receiving data, and finally writes the received data to a file and saves it. The program code of the sendreceivedata () function and event receivebtn_click (Object sender, system. eventargs e) function is as follows:
Private void receivebtn_click (Object sender, system. eventargs E)
{
// Receive data
Sendreceivedata ();
}
Private void sendreceivedata ()
{
// Create a socket for receiving data
Socket receivesocket = new socket (addressfamily. InterNetwork,
Sockettype. Stream, protocoltype. TCP );
// Set the address for receiving data
Ipendpoint hostpoint = new ipendpoint (IPaddress. parse ("127.0.0.1"), 80 );
// Set the port
Receivesocket. BIND (hostpoint );
// Listen
Receivesocket. Listen (2 );
// Set the size of the buffer for receiving data
Byte [] recdata = new byte [300000000];
Socket hostsocket = receivesocket. Accept ();
// Receive data
Hostsocket. Receive (recdata );
If (hostsocket. Receive (recdata)> 0)
{
// Display the received data
Labeldata. Text = "webpage in" + datetime. Now. tostring ()
+ "Received data size:" + hostsocket. Receive (recdata). tostring ();
}
// Create a new file to save the received data
Filestream = new filestream (server. mappath ("mynewfile. dat "),
Filemode. openorcreate );
Binarywriter writer = new binarywriter (filestream );
Writer. Write (recdata, 0, recdata. Length-1 );
// Close the file stream.
Filestream. Close ();
Writer. Close ();
// Close the socket for receiving data
Hostsocket. Shutdown (socketshutdown. Receive );
Hostsocket. Close ();
}
Running Effect
Set mysocket. aspx to the start page of the application. Press F5 to run the task. The initial interface shown in 12-21 is displayed.
Figure 12-21 initial interface of mysocket. aspx