In tcp c/S mode, point-to-point is implemented. One-to-multiple chats identify different TCP channels and send messages to corresponding TCP customers.

Source: Internet
Author: User

Client:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. net. Sockets;
Using system. net;
Using system. Threading;

Namespace mytcpchat
{
Public partial class form1: Form
{
Private thread th;
Socket C;
Public form1 ()
{
Initializecomponent ();
Control. checkforillegalcrossthreadcils = false;
}

Private void form1_load (Object sender, eventargs E)
{

}
Public void client ()
{
Try
{
Ipendpoint serveripep = new ipendpoint (IPaddress. parse (this. combobox1.text. Trim (), 5656 );
C = new socket (serveripep. addressfamily, sockettype. Stream, protocoltype. TCP );
C. Connect (endpoint) serveripep );
C. Send (system. Text. encoding. Default. getbytes (this. sendmessage. Text. Trim ()));
Byte [] DATA = new byte [2048];
While (true)
{
Int rect = C. Receive (data );
Byte [] chat = new byte [rect];
Buffer. blockcopy (data, 0, chat, 0, rect );
This. Message. appendtext (system. Text. encoding. Default. getstring (CHAT ));
}
}
Catch (exception ex)
{
// MessageBox. Show (ex. Message );
}

}

Private void button#click (Object sender, eventargs E)
{
Try
{

Th = new thread (New threadstart (client); // create a thread for listening
Th. Start (); // open a new thread
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
}
}
}

Server:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. net. Sockets;
Using system. net;
Using system. Threading;
Using system. collections;
Namespace mytcpserver
{
Public partial class form1: Form
{
Socket S;
Ipendpoint serveripep;
Private thread th;
Socket UC;
Private arraylist alsock ;//
Public form1 ()
{
Initializecomponent ();
Control. checkforillegalcrossthreadcils = false;
}
Public void server ()
{
Int Port = 5656;

Serveripep = new ipendpoint (IPaddress. Any, Port );
S = new socket (serveripep. addressfamily, sockettype. Stream, protocoltype. TCP );
S. BIND (endpoint) serveripep );
S. Listen (10 );
Alsock = new arraylist ();
While (true)
{
Try
{
UC = S. Accept ();
Alsock. Add (UC );
This. textbox1.appendtext (system. Convert. tostring (UC ));
Byte [] DATA = new byte [2048];
Int rect = UC. Receive (data );
Byte [] chat = new byte [rect];
Buffer. blockcopy (data, 0, chat, 0, rect );
This. mymessage. appendtext ("[" + UC. remoteendpoint. tostring () + "]" + system. Text. encoding. Default. getstring (CHAT ));

}
Catch (exception ex)
{
// MessageBox. Show (ex. Message );
}
}
}
Private void form1_load (Object sender, eventargs E)
{

}

Private void button#click (Object sender, eventargs E)
{
This. button1.enabled = false;
This. button2.enabled = true;
Th = new thread (New threadstart (server); // create a thread for listening
Th. Start (); // open a new thread
}

Private void button2_click (Object sender, eventargs E)
{
Try
{
This. button2.enabled = false;
This. button1.enabled = true;
S. Close ();
Th. Abort (); // terminate the thread

}
Catch (exception ex)
{
// MessageBox. Show (ex. Message );
}
}

Private void button3_click (Object sender, eventargs E)
{
If (UC = NULL)
{
MessageBox. Show ("You cannot chat with anyone, please tell the server to chat ");
}
Else
{
Int Index = int. parse (this. textbox2.text. Trim ());
Socket SC = (socket) alsock [Index];
// Send data
SC. Send (system. Text. encoding. Default. getbytes (this. answermessage. Text. Trim ()));
}

}
}
}

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.