Socket Basic Programming

xiaoxiao2021-03-06  60

I have recently been communicated with SOCKET, so I have organized some basic knowledge of the Socket and TCP protocol programming, so I will put it here, and will communicate some basic points for Socket asynchronous. Also organized. Socket Basic Programming

Server:

Using system.net;

Using system.net.sockets;

Using system.text;

Using system.threading;

Thread mythread;

Socket socket;

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

Try

{

Socket.close (); // Release Resources

mythread.abort (); // Abort thread

}

Catch {}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

Public static ipaddress getServerip ()

{

IPHOSTENTRY IEH = DNS.GETHOSTBYNAME (DNS.GETHOSTNAME ());

Return IEh.AddressList [0];

}

Private void beginlisten ()

{

Ipaddress serverip = get Serverip ();

IpendPoint IEP = New IpendPoint (ServerIP, 8000);

Socket = new socket (addressfamily.internetwork, sockettype.stream, protocoltype.tcp);

Byte [] bytemessage = new byte [100];

THIS.Label1.Text = IEP.TOString ();

Socket.bind (IEP);

// DO

While (True)

{

Try

{

Socket.listen (5);

Socket newsocket = socket.accept ();

Newsocket.Receive (ByteMessage);

String stime = datetime.now.toshorttimeString ();

String msg = stime ":" "Message from:";

Msg = news.remoteEndPoint.toString () encoding.default.getstring (byteMessage);

This.ListBox1.Items.Add (MSG);

}

Catch (socketexception ex)

{

THIS.LABEL1.TEXT = EX.TOSTRING ();

}

}

// while (byteMessage! = Null);

}

// Start listening

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Try

{

Mythread = New Thread (New ThreadStart (BeginListen));

mythread.start ();

}

Catch (System.exception ER)

{

Messagebox.show (Er.Message, "Complete", MessageBoxButtons.ok, MessageBoxicon.Stop;

}

Client:

Using system.net;

Using system.net.sockets;

Using system.text;

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Beginsend ();

}

Private void beginsend ()

{

String ip = this.txtip.text;

String port = this.txtport.text;

Ipaddress serverip = ipaddress.parse (IP);

INT ServerPort = Convert.Toint32 (Port);

IpendPoint IEP = New IpendPoint (ServerIP, Serverport);

Byte [] bytemessage;

// DO

// {

Socket Socket = New Socket (AddressFamily.InetNetwork, Sockettype.Stream, ProtocolType.tcp);

Socket.connect (IEP);

BYTEMESSAGE = Encoding.ASCII.GetBytes (TextBox1.text);

Socket.send (ByteMessage);

Socket.shutdown (socketshutdown.both);

Socket.close ();

//}

// while (byteMessage! = Null);

}

Transmit and receiving end based on TCP protocol

TCP protocol receiving end

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Use the StreamReader class

INT port = 8000; // Defines the listening port number

Private thread thisreadread; // Create a thread to listen to the port number, receive information

Private tcplistener tltcplisten; // Listening port number

Private bool blistener = true; // Set the marking bit, determine the listening state

Private networkStream nsstream; // Create a receiving basic data stream

Private streamreader srread;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Button button1;

Private system.windows.forms.listbox listbox1; // read data from the network base data stream

PRIVATE TCPCLIENT TCCLIENT;

Private void listen ()

{

Try

{

TLTCPLISTEN = New TCPListener (port); // Initialize the TCPListener instance with an 8000 port number

TLTCPListen.Start (); // Start listening

STATUSBAR1.TEXT = "Listening ...";

Tcclient = tltcplisten.acceptttcpclient (); // Request by TCP connection

NSStream = tcclient.getStream (); // Get network base data stream for sending, receive data SRREAD = new streamreader (NSSTREAM); // Initialize the StreamReader instance with the obtained network base data

Statusbar1.text = "It has been connected!";

While (blistener) // loop listening

{

String SMESSAGE = SRREAD.READLINE (); // Read a line of data from the network underlying data stream

IF (SMESSAGE == "STOP") // Determine if the TCP connection control code is disconnected

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close (); // Release Resources

Srread.close ();

Statusbar1.text = "The connection is closed!";

THTHREADREAD.ABORT (); // Abort thread

Return;

}

String Stime = DateTime.now.toshostTimeString (); // Get time when receiving data

ListBox1.Items.add (Stime " SMESSAGE);

}

}

Catch (System.Security.securityException)

{

Messagebox.show ("Listening Failed!", "Error");

}

}

// Start listening

Private void Button1_Click (Object Sender, System.Eventargs E)

{

ThthreadRead = New Thread (New ThreadStart (Listen));

THTHREADREAD.START (); // Start thread

Button1.enabled = false;

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

Try

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close ();

SRREAD.CLOSE (); // Release Resources

THTHREADREAD.ABORT (); // Abort thread

}

Catch {}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Using the streamwriter class

Using system.net; // use iPaddress class, iPhostentry class, etc.

Private streamwriter swwriter; // to send data to network basic data

Private networkStream nsstream; // Create a network base data of sending data

PRIVATE TCPCPCLIENT TCPCLIENT;

Private system.windows.Forms.Button button1;

Private system.windows.Forms.TextBox textBox1;

Private system.windows.Forms.Button Button2;

Private system.windows.Forms.TextBox textBox2;

Private system.windows.Forms.statusbar statusbar1; private system.windows.forms.label label1;

Private system.windows.forms.label label2; // Propose TCP connection to the remote host by it

Private bool tcpconnect = false; // Defines the identifier to indicate whether the TCP connection is established.

//connection

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Ipaddress ipremote;

Try

{

ipremote = ipaddress.parse (TextBox1.text);

}

Catch // judges the legality of a given IP address

{

Messagebox.show ("Enter IP address is not legal!", "Error Tip!");

Return;

}

IPHOSTENTRY iphost;

Try

{

iPhost = DNS.Resolve (TextBox1.text);

}

Catch // Determine whether the IP address corresponds to whether the host is online

{

Messagebox.show ("Remote Host is not online!", "Error Tip!");

Return;

}

String Shostname = iphost.hostname;

Try

{

TCPCLIENT TCPCLIENT = New TcpClient (ShostName, 8000); / / Propose TCP Connection Application for the 8000 port of the remote host

nsStream = tcpclient.getStream (); // By requesting, and obtaining network basic data flow for transferring data

SWRITER = New StreamWriter (NSSTream); // Initialize the StreamWriter instance using the obtained network base data stream

Button1.enabled = false;

Button2.enabled = True;

TcpConnect = true;

Statusbar1.text = "It has been connected!";

}

Catch

{

MessageBox.show ("Unable to establish a connection with the remote host 8000 port!", "Error Tip!");

Return;

}

}

//send

Private void button2_click (Object Sender, System.Eventargs E)

{

IF (TextBox2.text! = ")

{

SWRITER.WRITELINE (TextBox2.text); // Refresh the data in the current data stream

SWRITER.FLUSH ();

}

Else

{

Messagebox.show ("Send information cannot be empty!", "Error Tip!");

}

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

IF (TCPConnect)

{

SWRITER.WRITELINE ("STOP"); // Send control code

SWRITER.FLUSH (); // Refresh the data in the current data stream

NSSTream.close (); // Remove resources

SWRITER.CLOSE ();

}

IF (Disposing)

{

IF (Components! = NULL) {

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol receiving end

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Use the StreamReader class

INT port = 8000; // Defines the listening port number

Private thread thisreadread; // Create a thread to listen to the port number, receive information

Private tcplistener tltcplisten; // Listening port number

Private bool blistener = true; // Set the marking bit, determine the listening state

Private networkStream nsstream; // Create a receiving basic data stream

Private streamreader srread;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Button button1;

Private system.windows.forms.listbox listbox1; // read data from the network base data stream

PRIVATE TCPCLIENT TCCLIENT;

Private void listen ()

{

Try

{

TLTCPLISTEN = New TCPListener (port); // Initialize the TCPListener instance with an 8000 port number

TLTCPListen.Start (); // Start listening

STATUSBAR1.TEXT = "Listening ...";

Tcclient = tltcplisten.acceptttcpclient (); // Request by TCP connection

nsStream = tcclient.getStream (); // Get network base data flow used to send, receive data

SRREAD = New StreamReader (NSSTream); // Initialize the StreamReader instance with the obtained network basic data stream

Statusbar1.text = "It has been connected!";

While (blistener) // loop listening

{

String SMESSAGE = SRREAD.READLINE (); // Read a line of data from the network underlying data stream

IF (SMESSAGE == "STOP") // Determine if the TCP connection control code is disconnected

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close (); // Release Resources

Srread.close ();

Statusbar1.text = "The connection is closed!";

THTHREADREAD.ABORT (); // Abort thread

Return;

}

String Stime = DateTime.now.toshostTimeString (); // Get time when receiving data

ListBox1.Items.add (Stime " SMESSAGE);

}

}

Catch (System.Security.securityException)

{

Messagebox.show ("Listening Failed!", "Error");

}

}

// Start listening

Private void Button1_Click (Object Sender, System.Eventargs E)

{

ThthreadRead = New Thread (New ThreadStart (Listen));

THTHREADREAD.START (); // Start thread

Button1.enabled = false;

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

Try

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close ();

SRREAD.CLOSE (); // Release Resources

THTHREADREAD.ABORT (); // Abort thread

}

Catch {}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Using the streamwriter class

Using system.net; // use iPaddress class, iPhostentry class, etc.

Private streamwriter swwriter; // to send data to network basic data

Private networkStream nsstream; // Create a network base data of sending data

PRIVATE TCPCPCLIENT TCPCLIENT;

Private system.windows.Forms.Button button1;

Private system.windows.Forms.TextBox textBox1;

Private system.windows.Forms.Button Button2;

Private system.windows.Forms.TextBox textBox2;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Label label1;

Private system.windows.forms.label label2; // Propose TCP connection to the remote host by it

Private bool tcpconnect = false; // Defines the identifier to indicate whether the TCP connection is established.

//connection

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Ipaddress ipremote;

Try

{

ipremote = ipaddress.parse (TextBox1.text);

}

Catch // judges the legality of a given IP address

{

Messagebox.show ("Enter IP address is not legal!", "Error Tip!");

Return;

}

IPHOSTENTRY iphost;

Try

{

iPhost = DNS.Resolve (TextBox1.text);

}

Catch // Determine whether the IP address corresponds to whether the host is online

{

Messagebox.show ("Remote Host is not online!", "Error Tip!");

Return;

}

String Shostname = iphost.hostname;

Try

{

TCPCLIENT TCPCLIENT = New TcpClient (ShostName, 8000); / / Propose TCP Connection Application for the 8000 port of the remote host

nsStream = tcpclient.getStream (); // By requesting, and obtaining network basic data streaming of transmitting data SWRITER = New StreamWriter; // Initializing the streamwriter instance using the obtained network base data

Button1.enabled = false;

Button2.enabled = True;

TcpConnect = true;

Statusbar1.text = "It has been connected!";

}

Catch

{

MessageBox.show ("Unable to establish a connection with the remote host 8000 port!", "Error Tip!");

Return;

}

}

//send

Private void button2_click (Object Sender, System.Eventargs E)

{

IF (TextBox2.text! = ")

{

SWRITER.WRITELINE (TextBox2.text); // Refresh the data in the current data stream

SWRITER.FLUSH ();

}

Else

{

Messagebox.show ("Send information cannot be empty!", "Error Tip!");

}

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

IF (TCPConnect)

{

SWRITER.WRITELINE ("STOP"); // Send control code

SWRITER.FLUSH (); // Refresh the data in the current data stream

NSSTream.close (); // Remove resources

SWRITER.CLOSE ();

}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

Transmit and receiving end based on TCP protocol

TCP protocol receiving end

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Use the StreamReader class

INT port = 8000; // Defines the listening port number

Private thread thisreadread; // Create a thread to listen to the port number, receive information

Private tcplistener tltcplisten; // Listening port number

Private bool blistener = true; // Set the marking bit, determine the listening state

Private networkStream nsstream; // Create a receiving basic data stream

Private streamreader srread;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Button button1;

Private system.windows.forms.listbox listbox1; // read data from the network base data stream

PRIVATE TCPCLIENT TCCLIENT;

Private void listen ()

{

Try

{

TLTCPLISTEN = New TCPListener (port); // Initialize the TCPListener instance with an 8000 port number

TLTCPListen.Start (); // Start listening

Statusbar1.text = "Listening ..."; tcclient = tltcplisten.acceptttcpclient (); // Request by TCP connection

nsStream = tcclient.getStream (); // Get network base data flow used to send, receive data

SRREAD = New StreamReader (NSSTream); // Initialize the StreamReader instance with the obtained network basic data stream

Statusbar1.text = "It has been connected!";

While (blistener) // loop listening

{

String SMESSAGE = SRREAD.READLINE (); // Read a line of data from the network underlying data stream

IF (SMESSAGE == "STOP") // Determine if the TCP connection control code is disconnected

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close (); // Release Resources

Srread.close ();

Statusbar1.text = "The connection is closed!";

THTHREADREAD.ABORT (); // Abort thread

Return;

}

String Stime = DateTime.now.toshostTimeString (); // Get time when receiving data

ListBox1.Items.add (Stime " SMESSAGE);

}

}

Catch (System.Security.securityException)

{

Messagebox.show ("Listening Failed!", "Error");

}

}

// Start listening

Private void Button1_Click (Object Sender, System.Eventargs E)

{

ThthreadRead = New Thread (New ThreadStart (Listen));

THTHREADREAD.START (); // Start thread

Button1.enabled = false;

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

Try

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close ();

SRREAD.CLOSE (); // Release Resources

THTHREADREAD.ABORT (); // Abort thread

}

Catch {}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Using the streamwriter class

Using system.net; // use iPaddress class, iPhostentry class, etc.

Private streamwriter swwriter; // to send data to network basic data

Private networkStream nsstream; // Create a network base data of sending data

PRIVATE TCPCPCLIENT TCPCLIENT;

Private system.windows.Forms.Button button1;

Private system.windows.Forms.TextBox textBox1;

Private system.windows.Forms.Button Button2;

Private system.windows.Forms.TextBox textBox2;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Label label1;

Private system.windows.forms.label label2; // Propose TCP connection to the remote host by it

Private bool tcpconnect = false; // Defines the identifier to indicate whether the TCP connection is established.

//connection

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Ipaddress ipremote;

Try

{

ipremote = ipaddress.parse (TextBox1.text);

}

Catch // judges the legality of a given IP address

{

Messagebox.show ("Enter IP address is not legal!", "Error Tip!");

Return;

}

IPHOSTENTRY iphost;

Try

{

iPhost = DNS.Resolve (TextBox1.text);

}

Catch // Determine whether the IP address corresponds to whether the host is online

{

Messagebox.show ("Remote Host is not online!", "Error Tip!");

Return;

}

String Shostname = iphost.hostname;

Try

{

TCPCLIENT TCPCLIENT = New TcpClient (ShostName, 8000); / / Propose TCP Connection Application for the 8000 port of the remote host

nsStream = tcpclient.getStream (); // By requesting, and obtaining network basic data flow for transferring data

SWRITER = New StreamWriter (NSSTream); // Initialize the StreamWriter instance using the obtained network base data stream

Button1.enabled = false;

Button2.enabled = True;

TcpConnect = true;

Statusbar1.text = "It has been connected!";

}

Catch

{

MessageBox.show ("Unable to establish a connection with the remote host 8000 port!", "Error Tip!");

Return;

}

}

//send

Private void button2_click (Object Sender, System.Eventargs E)

{

IF (TextBox2.text! = ")

{

SWRITER.WRITELINE (TextBox2.text); // Refresh the data in the current data stream

SWRITER.FLUSH ();

}

Else

{

Messagebox.show ("Send information cannot be empty!", "Error Tip!");

}

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

IF (TCPConnect)

{

SWRITER.WRITELINE ("STOP"); // Send control code

Swriter.flush (); // Refresh the data in the current data stream nSSTream.close (); // Clear resource

SWRITER.CLOSE ();

}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol receiving end

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Use the StreamReader class

INT port = 8000; // Defines the listening port number

Private thread thisreadread; // Create a thread to listen to the port number, receive information

Private tcplistener tltcplisten; // Listening port number

Private bool blistener = true; // Set the marking bit, determine the listening state

Private networkStream nsstream; // Create a receiving basic data stream

Private streamreader srread;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Button button1;

Private system.windows.forms.listbox listbox1; // read data from the network base data stream

PRIVATE TCPCLIENT TCCLIENT;

Private void listen ()

{

Try

{

TLTCPLISTEN = New TCPListener (port); // Initialize the TCPListener instance with an 8000 port number

TLTCPListen.Start (); // Start listening

STATUSBAR1.TEXT = "Listening ...";

Tcclient = tltcplisten.acceptttcpclient (); // Request by TCP connection

nsStream = tcclient.getStream (); // Get network base data flow used to send, receive data

SRREAD = New StreamReader (NSSTream); // Initialize the StreamReader instance with the obtained network basic data stream

Statusbar1.text = "It has been connected!";

While (blistener) // loop listening

{

String SMESSAGE = SRREAD.READLINE (); // Read a line of data from the network underlying data stream

IF (SMESSAGE == "STOP") // Determine if the TCP connection control code is disconnected

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close (); // Release Resources

Srread.close ();

Statusbar1.text = "The connection is closed!";

THTHREADREAD.ABORT (); // Abort thread

Return;

}

String Stime = DateTime.now.toshostTimeString (); // Get time when receiving data

ListBox1.Items.add (Stime " SMESSAGE);

}

}

Catch (System.Security.securityException)

{

Messagebox.show ("Listening Failed!", "Error");

}

// Start listening

Private void Button1_Click (Object Sender, System.Eventargs E)

{

ThthreadRead = New Thread (New ThreadStart (Listen));

THTHREADREAD.START (); // Start thread

Button1.enabled = false;

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

Try

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close ();

SRREAD.CLOSE (); // Release Resources

THTHREADREAD.ABORT (); // Abort thread

}

Catch {}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Using the streamwriter class

Using system.net; // use iPaddress class, iPhostentry class, etc.

Private streamwriter swwriter; // to send data to network basic data

Private networkStream nsstream; // Create a network base data of sending data

PRIVATE TCPCPCLIENT TCPCLIENT;

Private system.windows.Forms.Button button1;

Private system.windows.Forms.TextBox textBox1;

Private system.windows.Forms.Button Button2;

Private system.windows.Forms.TextBox textBox2;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Label label1;

Private system.windows.forms.label label2; // Propose TCP connection to the remote host by it

Private bool tcpconnect = false; // Defines the identifier to indicate whether the TCP connection is established.

//connection

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Ipaddress ipremote;

Try

{

ipremote = ipaddress.parse (TextBox1.text);

}

Catch // judges the legality of a given IP address

{

Messagebox.show ("Enter IP address is not legal!", "Error Tip!");

Return;

}

IPHOSTENTRY iphost;

Try

{

iPhost = DNS.Resolve (TextBox1.text);

}

Catch // Determine whether the IP address corresponds to whether the host is online

{

Messagebox.show ("Remote Host is not online!", "Error Tip!");

Return;

}

String Shostname = iphost.hostname; try

{

TCPCLIENT TCPCLIENT = New TcpClient (ShostName, 8000); / / Propose TCP Connection Application for the 8000 port of the remote host

nsStream = tcpclient.getStream (); // By requesting, and obtaining network basic data flow for transferring data

SWRITER = New StreamWriter (NSSTream); // Initialize the StreamWriter instance using the obtained network base data stream

Button1.enabled = false;

Button2.enabled = True;

TcpConnect = true;

Statusbar1.text = "It has been connected!";

}

Catch

{

MessageBox.show ("Unable to establish a connection with the remote host 8000 port!", "Error Tip!");

Return;

}

}

//send

Private void button2_click (Object Sender, System.Eventargs E)

{

IF (TextBox2.text! = ")

{

SWRITER.WRITELINE (TextBox2.text); // Refresh the data in the current data stream

SWRITER.FLUSH ();

}

Else

{

Messagebox.show ("Send information cannot be empty!", "Error Tip!");

}

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

IF (TCPConnect)

{

SWRITER.WRITELINE ("STOP"); // Send control code

SWRITER.FLUSH (); // Refresh the data in the current data stream

NSSTream.close (); // Remove resources

SWRITER.CLOSE ();

}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

Server:

Using system.net;

Using system.net.sockets;

Using system.text;

Using system.threading;

Thread mythread;

Socket socket;

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

Try

{

Socket.close (); // Release Resources

mythread.abort (); // Abort thread

}

Catch {}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

Public static ipaddress getServerip ()

{

IPHOSTENTRY IEH = DNS.GETHOSTBYNAME (DNS.GETHOSTNAME ());

Return IEh.AddressList [0];

}

Private void beginlisten ()

{

Ipaddress serverip = get Serverip ();

IpendPoint IEP = New IpendPoint (ServerIP, 8000); socket = new socket (addressfamily.internetwork, sockettype.stream, protocoltype.tcp);

Byte [] bytemessage = new byte [100];

THIS.Label1.Text = IEP.TOString ();

Socket.bind (IEP);

// DO

While (True)

{

Try

{

Socket.listen (5);

Socket newsocket = socket.accept ();

Newsocket.Receive (ByteMessage);

String stime = datetime.now.toshorttimeString ();

String msg = stime ":" "Message from:";

Msg = news.remoteEndPoint.toString () encoding.default.getstring (byteMessage);

This.ListBox1.Items.Add (MSG);

}

Catch (socketexception ex)

{

THIS.LABEL1.TEXT = EX.TOSTRING ();

}

}

// while (byteMessage! = Null);

}

// Start listening

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Try

{

Mythread = New Thread (New ThreadStart (BeginListen));

mythread.start ();

}

Catch (System.exception ER)

{

Messagebox.show (Er.Message, "Complete", MessageBoxButtons.ok, MessageBoxicon.Stop;

}

}

Client:

Using system.net;

Using system.net.sockets;

Using system.text;

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Beginsend ();

}

Private void beginsend ()

{

String ip = this.txtip.text;

String port = this.txtport.text;

Ipaddress serverip = ipaddress.parse (IP);

INT ServerPort = Convert.Toint32 (Port);

IpendPoint IEP = New IpendPoint (ServerIP, Serverport);

Byte [] bytemessage;

// DO

// {

Socket Socket = New Socket (AddressFamily.InetNetwork, Sockettype.Stream, ProtocolType.tcp);

Socket.connect (IEP);

BYTEMESSAGE = Encoding.ASCII.GetBytes (TextBox1.text);

Socket.send (ByteMessage);

Socket.shutdown (socketshutdown.both);

Socket.close ();

//} // while (byteMessage! = Null);

}

Transmit and receiving end based on TCP protocol

TCP protocol receiving end

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Use the StreamReader class

INT port = 8000; // Defines the listening port number

Private thread thisreadread; // Create a thread to listen to the port number, receive information

Private tcplistener tltcplisten; // Listening port number

Private bool blistener = true; // Set the marking bit, determine the listening state

Private networkStream nsstream; // Create a receiving basic data stream

Private streamreader srread;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Button button1;

Private system.windows.forms.listbox listbox1; // read data from the network base data stream

PRIVATE TCPCLIENT TCCLIENT;

Private void listen ()

{

Try

{

TLTCPLISTEN = New TCPListener (port); // Initialize the TCPListener instance with an 8000 port number

TLTCPListen.Start (); // Start listening

STATUSBAR1.TEXT = "Listening ...";

Tcclient = tltcplisten.acceptttcpclient (); // Request by TCP connection

nsStream = tcclient.getStream (); // Get network base data flow used to send, receive data

SRREAD = New StreamReader (NSSTream); // Initialize the StreamReader instance with the obtained network basic data stream

Statusbar1.text = "It has been connected!";

While (blistener) // loop listening

{

String SMESSAGE = SRREAD.READLINE (); // Read a line of data from the network underlying data stream

IF (SMESSAGE == "STOP") // Determine if the TCP connection control code is disconnected

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close (); // Release Resources

Srread.close ();

Statusbar1.text = "The connection is closed!";

THTHREADREAD.ABORT (); // Abort thread

Return;

}

String Stime = DateTime.now.toshostTimeString (); // Get time when receiving data

ListBox1.Items.add (Stime " SMESSAGE);

}

}

Catch (System.Security.securityException)

{

Messagebox.show ("Listening Failed!", "Error");

}

}

// Start listening

Private void Button1_Click (Object Sender, System.Eventargs E)

{

ThthreadRead = New Thread (New ThreadStart (Listen)); THTHREADREAD.START (); // Start thread

Button1.enabled = false;

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

Try

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close ();

SRREAD.CLOSE (); // Release Resources

THTHREADREAD.ABORT (); // Abort thread

}

Catch {}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Using the streamwriter class

Using system.net; // use iPaddress class, iPhostentry class, etc.

Private streamwriter swwriter; // to send data to network basic data

Private networkStream nsstream; // Create a network base data of sending data

PRIVATE TCPCPCLIENT TCPCLIENT;

Private system.windows.Forms.Button button1;

Private system.windows.Forms.TextBox textBox1;

Private system.windows.Forms.Button Button2;

Private system.windows.Forms.TextBox textBox2;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Label label1;

Private system.windows.forms.label label2; // Propose TCP connection to the remote host by it

Private bool tcpconnect = false; // Defines the identifier to indicate whether the TCP connection is established.

//connection

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Ipaddress ipremote;

Try

{

ipremote = ipaddress.parse (TextBox1.text);

}

Catch // judges the legality of a given IP address

{

Messagebox.show ("Enter IP address is not legal!", "Error Tip!");

Return;

}

IPHOSTENTRY iphost;

Try

{

iPhost = DNS.Resolve (TextBox1.text);

}

Catch // Determine whether the IP address corresponds to whether the host is online

{

Messagebox.show ("Remote Host is not online!", "Error Tip!");

Return;

}

String Shostname = iphost.hostname;

Try

{

TCPCLIENT TCPCLIENT = New TcpClient (ShostName, 8000); / / Propose TCP Connection Application for the 8000 port of the remote host

nsStream = tcpclient.getStream (); // By requesting, and obtaining network basic data streaming of transmitting data SWRITER = New StreamWriter; // Initializing the streamwriter instance using the obtained network base data

Button1.enabled = false;

Button2.enabled = True;

TcpConnect = true;

Statusbar1.text = "It has been connected!";

}

Catch

{

MessageBox.show ("Unable to establish a connection with the remote host 8000 port!", "Error Tip!");

Return;

}

}

//send

Private void button2_click (Object Sender, System.Eventargs E)

{

IF (TextBox2.text! = ")

{

SWRITER.WRITELINE (TextBox2.text); // Refresh the data in the current data stream

SWRITER.FLUSH ();

}

Else

{

Messagebox.show ("Send information cannot be empty!", "Error Tip!");

}

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

IF (TCPConnect)

{

SWRITER.WRITELINE ("STOP"); // Send control code

SWRITER.FLUSH (); // Refresh the data in the current data stream

NSSTream.close (); // Remove resources

SWRITER.CLOSE ();

}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol receiving end

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Use the StreamReader class

INT port = 8000; // Defines the listening port number

Private thread thisreadread; // Create a thread to listen to the port number, receive information

Private tcplistener tltcplisten; // Listening port number

Private bool blistener = true; // Set the marking bit, determine the listening state

Private networkStream nsstream; // Create a receiving basic data stream

Private streamreader srread;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Button button1;

Private system.windows.forms.listbox listbox1; // read data from the network base data stream

PRIVATE TCPCLIENT TCCLIENT;

Private void listen ()

{

Try

{

TLTCPLISTEN = New TCPListener (port); // Initialize the TCPListener instance with an 8000 port number

TLTCPListen.Start (); // Start listening

Statusbar1.text = "Listening ..."; tcclient = tltcplisten.acceptttcpclient (); // Request by TCP connection

nsStream = tcclient.getStream (); // Get network base data flow used to send, receive data

SRREAD = New StreamReader (NSSTream); // Initialize the StreamReader instance with the obtained network basic data stream

Statusbar1.text = "It has been connected!";

While (blistener) // loop listening

{

String SMESSAGE = SRREAD.READLINE (); // Read a line of data from the network underlying data stream

IF (SMESSAGE == "STOP") // Determine if the TCP connection control code is disconnected

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close (); // Release Resources

Srread.close ();

Statusbar1.text = "The connection is closed!";

THTHREADREAD.ABORT (); // Abort thread

Return;

}

String Stime = DateTime.now.toshostTimeString (); // Get time when receiving data

ListBox1.Items.add (Stime " SMESSAGE);

}

}

Catch (System.Security.securityException)

{

Messagebox.show ("Listening Failed!", "Error");

}

}

// Start listening

Private void Button1_Click (Object Sender, System.Eventargs E)

{

ThthreadRead = New Thread (New ThreadStart (Listen));

THTHREADREAD.START (); // Start thread

Button1.enabled = false;

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

Try

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close ();

SRREAD.CLOSE (); // Release Resources

THTHREADREAD.ABORT (); // Abort thread

}

Catch {}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Using the streamwriter class

Using system.net; // use iPaddress class, iPhostentry class, etc.

Private streamwriter swwriter; // to send data to network basic data

Private networkStream nsstream; // Create a network base data of sending data

PRIVATE TCPCPCLIENT TCPCLIENT;

Private system.windows.Forms.Button button1;

Private system.windows.Forms.TextBox textBox1;

Private system.windows.Forms.Button Button2;

Private system.windows.Forms.TextBox textBox2;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Label label1;

Private system.windows.forms.label label2; // Propose TCP connection to the remote host by it

Private bool tcpconnect = false; // Defines the identifier to indicate whether the TCP connection is established.

//connection

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Ipaddress ipremote;

Try

{

ipremote = ipaddress.parse (TextBox1.text);

}

Catch // judges the legality of a given IP address

{

Messagebox.show ("Enter IP address is not legal!", "Error Tip!");

Return;

}

IPHOSTENTRY iphost;

Try

{

iPhost = DNS.Resolve (TextBox1.text);

}

Catch // Determine whether the IP address corresponds to whether the host is online

{

Messagebox.show ("Remote Host is not online!", "Error Tip!");

Return;

}

String Shostname = iphost.hostname;

Try

{

TCPCLIENT TCPCLIENT = New TcpClient (ShostName, 8000); / / Propose TCP Connection Application for the 8000 port of the remote host

nsStream = tcpclient.getStream (); // By requesting, and obtaining network basic data flow for transferring data

SWRITER = New StreamWriter (NSSTream); // Initialize the StreamWriter instance using the obtained network base data stream

Button1.enabled = false;

Button2.enabled = True;

TcpConnect = true;

Statusbar1.text = "It has been connected!";

}

Catch

{

MessageBox.show ("Unable to establish a connection with the remote host 8000 port!", "Error Tip!");

Return;

}

}

//send

Private void button2_click (Object Sender, System.Eventargs E)

{

IF (TextBox2.text! = ")

{

SWRITER.WRITELINE (TextBox2.text); // Refresh the data in the current data stream

SWRITER.FLUSH ();

}

Else

{

Messagebox.show ("Send information cannot be empty!", "Error Tip!");

}

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

IF (TCPConnect)

{

SWRITER.WRITELINE ("STOP"); // Send control code

Swriter.flush (); // Refresh the data in the current data stream nSSTream.close (); // Clear resource

SWRITER.CLOSE ();

}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

Transmit and receiving end based on TCP protocol

TCP protocol receiving end

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Use the StreamReader class

INT port = 8000; // Defines the listening port number

Private thread thisreadread; // Create a thread to listen to the port number, receive information

Private tcplistener tltcplisten; // Listening port number

Private bool blistener = true; // Set the marking bit, determine the listening state

Private networkStream nsstream; // Create a receiving basic data stream

Private streamreader srread;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Button button1;

Private system.windows.forms.listbox listbox1; // read data from the network base data stream

PRIVATE TCPCLIENT TCCLIENT;

Private void listen ()

{

Try

{

TLTCPLISTEN = New TCPListener (port); // Initialize the TCPListener instance with an 8000 port number

TLTCPListen.Start (); // Start listening

STATUSBAR1.TEXT = "Listening ...";

Tcclient = tltcplisten.acceptttcpclient (); // Request by TCP connection

nsStream = tcclient.getStream (); // Get network base data flow used to send, receive data

SRREAD = New StreamReader (NSSTream); // Initialize the StreamReader instance with the obtained network basic data stream

Statusbar1.text = "It has been connected!";

While (blistener) // loop listening

{

String SMESSAGE = SRREAD.READLINE (); // Read a line of data from the network underlying data stream

IF (SMESSAGE == "STOP") // Determine if the TCP connection control code is disconnected

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close (); // Release Resources

Srread.close ();

Statusbar1.text = "The connection is closed!";

THTHREADREAD.ABORT (); // Abort thread

Return;

}

String Stime = DateTime.now.toshostTimeString (); // Get time when receiving data

ListBox1.Items.add (Stime " SMESSAGE);

}

}

Catch (System.Security.securityException)

{

Messagebox.show ("Listening Failed!", "Error");

}

// Start listening

Private void Button1_Click (Object Sender, System.Eventargs E)

{

ThthreadRead = New Thread (New ThreadStart (Listen));

THTHREADREAD.START (); // Start thread

Button1.enabled = false;

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

Try

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close ();

SRREAD.CLOSE (); // Release Resources

THTHREADREAD.ABORT (); // Abort thread

}

Catch {}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Using the streamwriter class

Using system.net; // use iPaddress class, iPhostentry class, etc.

Private streamwriter swwriter; // to send data to network basic data

Private networkStream nsstream; // Create a network base data of sending data

PRIVATE TCPCPCLIENT TCPCLIENT;

Private system.windows.Forms.Button button1;

Private system.windows.Forms.TextBox textBox1;

Private system.windows.Forms.Button Button2;

Private system.windows.Forms.TextBox textBox2;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Label label1;

Private system.windows.forms.label label2; // Propose TCP connection to the remote host by it

Private bool tcpconnect = false; // Defines the identifier to indicate whether the TCP connection is established.

//connection

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Ipaddress ipremote;

Try

{

ipremote = ipaddress.parse (TextBox1.text);

}

Catch // judges the legality of a given IP address

{

Messagebox.show ("Enter IP address is not legal!", "Error Tip!");

Return;

}

IPHOSTENTRY iphost;

Try

{

iPhost = DNS.Resolve (TextBox1.text);

}

Catch // Determine whether the IP address corresponds to whether the host is online

{

Messagebox.show ("Remote Host is not online!", "Error Tip!");

Return;

}

String Shostname = iphost.hostname; try

{

TCPCLIENT TCPCLIENT = New TcpClient (ShostName, 8000); / / Propose TCP Connection Application for the 8000 port of the remote host

nsStream = tcpclient.getStream (); // By requesting, and obtaining network basic data flow for transferring data

SWRITER = New StreamWriter (NSSTream); // Initialize the StreamWriter instance using the obtained network base data stream

Button1.enabled = false;

Button2.enabled = True;

TcpConnect = true;

Statusbar1.text = "It has been connected!";

}

Catch

{

MessageBox.show ("Unable to establish a connection with the remote host 8000 port!", "Error Tip!");

Return;

}

}

//send

Private void button2_click (Object Sender, System.Eventargs E)

{

IF (TextBox2.text! = ")

{

SWRITER.WRITELINE (TextBox2.text); // Refresh the data in the current data stream

SWRITER.FLUSH ();

}

Else

{

Messagebox.show ("Send information cannot be empty!", "Error Tip!");

}

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

IF (TCPConnect)

{

SWRITER.WRITELINE ("STOP"); // Send control code

SWRITER.FLUSH (); // Refresh the data in the current data stream

NSSTream.close (); // Remove resources

SWRITER.CLOSE ();

}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol receiving end

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Use the StreamReader class

INT port = 8000; // Defines the listening port number

Private thread thisreadread; // Create a thread to listen to the port number, receive information

Private tcplistener tltcplisten; // Listening port number

Private bool blistener = true; // Set the marking bit, determine the listening state

Private networkStream nsstream; // Create a receiving basic data stream

Private streamreader srread;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Button button1;

Private system.windows.forms.listbox listbox1; // read data from the network base data stream

PRIVATE TCPCLIENT TCCLIENT;

Private void listen ()

{

Try

{

TLTCPLISTEN = New TCPListener (port); // Initialize the TCPListener instance with an 8000 port number

TLTCPListen.Start (); // Start listening

STATUSBAR1.TEXT = "Listening ...";

Tcclient = tltcplisten.acceptttcpclient (); // Request by TCP connection

nsStream = tcclient.getStream (); // Get network base data flow used to send, receive data

SRREAD = New StreamReader (NSSTream); // Initialize the StreamReader instance with the obtained network basic data stream

Statusbar1.text = "It has been connected!";

While (blistener) // loop listening

{

String SMESSAGE = SRREAD.READLINE (); // Read a line of data from the network underlying data stream

IF (SMESSAGE == "STOP") // Determine if the TCP connection control code is disconnected

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close (); // Release Resources

Srread.close ();

Statusbar1.text = "The connection is closed!";

THTHREADREAD.ABORT (); // Abort thread

Return;

}

String Stime = DateTime.now.toshostTimeString (); // Get time when receiving data

ListBox1.Items.add (Stime " SMESSAGE);

}

}

Catch (System.Security.securityException)

{

Messagebox.show ("Listening Failed!", "Error");

}

}

// Start listening

Private void Button1_Click (Object Sender, System.Eventargs E)

{

ThthreadRead = New Thread (New ThreadStart (Listen));

THTHREADREAD.START (); // Start thread

Button1.enabled = false;

}

// Clean all the resources being used.

Protected Override Void Dispose (Bool Disposing)

{

Try

{

TLTCPLISTEN.STOP (); // Turn the listener

NSSTream.close ();

SRREAD.CLOSE (); // Release Resources

THTHREADREAD.ABORT (); // Abort thread

}

Catch {}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

TCP protocol

Using system.net.sockets; // Used to TCPListen

Using system.threading; // Using thread

Using system.io; // Using the streamwriter class

Using system.net; // use iPaddress class, iPhostentry class, etc.

Private streamwriter swwriter; // to send data to network basic data

Private networkStream nsstream; // Create a network base data of sending data

Private tclient tclient; private system.windows.Forms.Button Button1;

Private system.windows.Forms.TextBox textBox1;

Private system.windows.Forms.Button Button2;

Private system.windows.Forms.TextBox textBox2;

Private system.windows.Forms.statusbar statusbar1;

Private system.windows.Forms.Label label1;

Private system.windows.forms.label label2; // Propose TCP connection to the remote host by it

Private bool tcpconnect = false; // Defines the identifier to indicate whether the TCP connection is established.

//connection

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Ipaddress ipremote;

Try

{

ipremote = ipaddress.parse (TextBox1.text);

}

Catch // judges the legality of a given IP address

{

Messagebox.show ("Enter IP address is not legal!", "Error Tip!");

Return;

}

IPHOSTENTRY iphost;

Try

{

iPhost = DNS.Resolve (TextBox1.text);

}

Catch // Determine whether the IP address corresponds to whether the host is online

{

Messagebox.show ("Remote Host is not online!", "Error Tip!");

Return;

}

String Shostname = iphost.hostname;

Try

{

TCPCLIENT TCPCLIENT = New TcpClient (ShostName, 8000); / / Propose TCP Connection Application for the 8000 port of the remote host

nsStream = tcpclient.getStream (); // By requesting, and obtaining network basic data flow for transferring data

SWRITER = New StreamWriter (NSSTream); // Initialize the StreamWriter instance using the obtained network base data stream

Button1.enabled = false;

Button2.enabled = True;

TcpConnect = true;

Statusbar1.text = "It has been connected!";

}

Catch

{

MessageBox.show ("Unable to establish a connection with the remote host 8000 port!", "Error Tip!");

Return;

}

}

//send

Private void button2_click (Object Sender, System.Eventargs E)

{

IF (TextBox2.text! = ")

{

SWRITER.WRITELINE (TextBox2.text); // Refresh the data in the current data stream

SWRITER.FLUSH ();

}

Else

{

Messagebox.show ("Send information cannot be empty!", "Error Tip!");

}

}

// Clean all the resources being used.

Protected Override Void Dispose (BOOL Disposing) {

IF (TCPConnect)

{

SWRITER.WRITELINE ("STOP"); // Send control code

SWRITER.FLUSH (); // Refresh the data in the current data stream

NSSTream.close (); // Remove resources

SWRITER.CLOSE ();

}

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

转载请注明原文地址:https://www.9cbs.com/read-84470.html

New Post(0)