A simple Socket Client sends messages to the server program

Source: Internet
Author: User

ServerCode:

 

Using system;

Using system. text;

Using system. Windows. forms;

Using system. net;

Using system. net. Sockets;

Using system. Threading;

Namespace tcpreceive

{

/// <Summary>

/// Server

/// </Summary>

Public partial class frmreceive: Form

{

Tcplistener listener = new tcplistener (New ipendpoint (IPaddress. Any, 2222); // port 2222

Public frmreceive ()

{

Initializecomponent ();

Listener. Start (); // start listening port

Thread acceptthread = new thread (New threadstart (acceptworkthread ));

Acceptthread. Start (); // receives client requests

}

 

/// <Summary>

/// Receive and decode

/// </Summary>

Private void acceptworkthread ()

{

Socket socket = listener. acceptsocket ();

Byte [] buffer = new byte [1024];

While (true)

{

Int receivecount = socket. Receive (buffer );

If (receivecount> 0)

{

String recstring = encoding. utf8.getstring (buffer, 0, receivecount); // decoded

Showmsg (recstring );

}

Else

{

Socket. Close ();

Break;

}

}

}

 

/// <Summary>

/// Display the received message content

/// </Summary>

/// <Param name = "text"> </param>

Public void showmsg (string text)

{

Rboxrecevie. Text = text;

}

}

}

 

-----------------------------------------------------------------------

 

Client code:

Using system;

 

Using system. text;

Using system. Windows. forms;

Using system. net;

Using system. net. Sockets;

Namespace tcpsend

{

/// <Summary>

/// Client

/// </Summary>

Public partial class frmsend: Form

{

Socket socket = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );

Public frmsend ()

{

Initializecomponent ();

}

 

/// <Summary>

/// Connect to the server

/// </Summary>

/// <Param name = "sender"> </param>

/// <Param name = "E"> </param>

Private void btnconn_click (Object sender, eventargs E)

{

IPaddress serverip = IPaddress. parse ("192.168.1.102 ");

Ipendpoint IEP = new ipendpoint (serverip, 2222 );

Socket. Connect (IEP); // connect to the server

}

 

/// <Summary>

/// Send a message

/// </Summary>

/// <Param name = "sender"> </param>

/// <Param name = "E"> </param>

Private void btnsend_click (Object sender, eventargs E)

{

If (socket. Connected)

Beginsend ();

Else

MessageBox. Show ("connect to the server first! "," Prompt ");

}

 

Private void beginsend ()

{

Byte [] bytemessage;

Bytemessage = encoding. utf8.getbytes (rboxcontent. Text );

Socket. Send (bytemessage );

}

 

/// <Summary>

/// Release the socket when the window is closed

/// </Summary>

/// <Param name = "sender"> </param>

/// <Param name = "E"> </param>

Private void frmsend_formclosing (Object sender, formclosingeventargs E)

{

If (socket. Connected)

Socket. Shutdown (socketshutdown. Both );

Socket. Close ();

}

}

}

 

 

Source program download

 

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.