Here we will introduce the tcpclient class and socket class. We will take two examples to analyze the differences between these two classes in receiving and sending data.
TCP example:
TCP sender to send data to the server
Using system;
Using system. Windows. forms;
Using system. net;
Using system. net. Sockets;
Using system. IO;
Namespace tcpsender
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
{
Tcpclient client = new tcpclient (textbox1.text, int32.parse (textbox2.text); // textbox1 is used to enter the address, and textbox2 is used to enter the access port
Networkstream NS = client. getstream ();
Filestream FS = file. Open ("form1.cs", filemode. Open );
Int DATA = FS. readbyte ();
While (Data! =-1)
{
NS. writebyte (byte) data );
Data = FS. readbyte ();
}
FS. Close ();
NS. Close ();
Client. Close ();
}
}
}
TCP receiver, which listens to and receives data
Using system;
Using system. Windows. forms;
Using system. Threading;
Using system. net;
Using system. net. Sockets;
Using system. IO;
Namespace tcpreceiver
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
Thread thread = new thread (New threadstart (Listen ));
Thread. Start ();
}
Public void listen ()
{
IPaddress localaddr = IPaddress. parse ("127.0.0.1 ");
Int32 Port = 2112;
Tcplistener listener = new tcplistener (localaddr, Port );
Listener. Start ();
Tcpclient = listener. accepttcpclient ();
Networkstream NS = tcpclient. getstream ();
Streamreader sr = new streamreader (NS );
String result = Sr. readtoend ();
Invoke (New updatedisplaydelegate (updatedisplay), new object [] {result });
Tcpclient. Close ();
Listener. Stop ();
}
Public void updatedisplay (string text)
{
Textbox1.text = text;
}
Protected Delegate void updatedisplaydelegate (string text );
}
}
The execution result is as follows:
Socket example
Socket information Sender:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. net. Sockets;
Using system. net;
Namespace socketsenderconsole
{
Class Program
{
Static void main (string [] ARGs)
{
Byte [] receivedbytes = new byte [1024];
// Use the local host to create a ipendpoint, Which is connection end point
Iphostentry iphost = DNS. Resolve ("127.0.0.1 ");
IPaddress = iphost. Addresslist [0];
Ipendpoint = new ipendpoint (IPaddress, 2112 );
Console. writeline ("starting: Creating socket object .");
// To create a sender
Socket sender = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
Sender. Connect (ipendpoint );
String sendingmessage = "Hello World socket test ";
Console. writeline ("creating message: Hello World socket test ");
Byte [] forwardmessage = encoding. ASCII. getbytes (sendingmessage + "[Final]");
// Send messages
Sender. Send (forwardmessage );
// Receive the Server Response
Int totalbytesreceived = sender. Receive (receivedbytes );
Console. writeline ("message provided from server: {0}", encoding. ASCII. getstring (receivedbytes, 0, totalbytesreceived ));
// Shut down and Dispose
Sender. Shutdown (socketshutdown. Both );
Sender. Close ();
Console. Readline ();
}
}
}
Socket information listener:
Using system. text;
Using system. net. Sockets;
Using system. net;
Namespace sockedlistener
{
Class Program
{
Static void main (string [] ARGs)
{
Console. writeline ("starting: Creating socket object .");
// The socket class allows you to perform both synchronous and asynchronous data transfer using any of the communication protocols listed in the protocoltype enumeration.
Socket listener = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
Listener. BIND (New ipendpoint (IPaddress. Any, 2112 ));
Listener. Listen (10 );
While (true)
{
Console. writeline ("waiting for connection on port 2112 ");
// The accept method processes any incoming connection requests and returns a socket that you can use to communicate data with the remote host.
Socket socket = listener. Accept ();
String receivedvalue = string. empty;
While (true)
{
Byte [] receivedbytes = new byte [1024];
Int numbytes = socket. Receive (receivedbytes );
Console. writeline ("logging ing ...");
Receivedvalue + = encoding. ASCII. getstring (receivedbytes, 0, numbytes );
If (receivedvalue. indexof ("[Final]")>-1)
{
Break;
}
}
Console. writeline ("received value: {0}", receivedvalue );
String replyvalue = "message successfully received .";
Byte [] replymessage = encoding. ASCII. getbytes (replyvalue );
Socket. Send (replymessage );
Socket. Shutdown (socketshutdown. Both );
Socket. Close ();
}
Listener. Close ();
}
}
Execution result: