Simple multi-user chat (C #. Socket ).

Source: Internet
Author: User

Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;

Using System. Threading;
Using System. Net. Sockets;
Using System. Net;
Namespace Chat_Server
{
/// <Summary>
/// Summary of Form1.
/// </Summary>
Public class Form1: System. Windows. Forms. Form
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;

Static int listenport = 6666;
Socket clientsocket;
Private System. Windows. Forms. ListBox lbClients;
ArrayList clients;
Private System. Windows. Forms. Button button1;
Thread clientservice;
Private System. Windows. Forms. Label label1;
Thread threadListen;

Public Form1 ()
{

InitializeComponent ();

}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{

If (clientservice! = Null)
{
Clientservice. Abort ();
}
If (threadListen! = Null)
{
Try
{
ThreadListen. Abort ();
}
Catch (Exception ex)
{
ThreadListen = null;
}
}

If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );

}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. lbClients = new System. Windows. Forms. ListBox ();
This. button1 = new System. Windows. Forms. Button ();
This. label1 = new System. Windows. Forms. Label ();
This. SuspendLayout ();
//
// LbClients
//
This. lbClients. ItemHeight = 12;
This. lbClients. Location = new System. Drawing. Point (16, 24 );
This. lbClients. Name = "lbClients ";
This. lbClients. Size = new System. Drawing. Size (184,268 );
This. lbClients. TabIndex = 0;
//
// Button1
//
This. button1.Location = new System. Drawing. Point (272, 56 );
This. button1.Name = "button1 ";
This. button1.TabIndex = 1;
This. button1.Text = "button1 ";
This. button1.Click + = new System. EventHandler (this. button#click );
//
// Label1
//
This. label1.Location = new System. Drawing. Point (240,136 );
This. label1.Name = "label1 ";
This. label1.Size = new System. Drawing. Size (120, 32 );
This. label1.TabIndex = 2;
This. label1.Text = "label1 ";
//
// Form1
//
This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
This. ClientSize = new System. Drawing. Size (368,309 );
This. Controls. Add (this. label1 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. lbClients );
This. Name = "Form1 ";
This. Text = "Form1 ";
This. Load + = new System. EventHandler (this. form#load );
This. ResumeLayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. Run (new Form1 ());
}

Private void StartListening ()
{
TcpListener listener = new TcpListener (listenport );
Listener. Start ();
Label1.Text = "listening ....";
While (true)
{
Try
{

Socket s = listener. AcceptSocket ();
Clientsocket = s;
Clientservice = new Thread (new ThreadStart (ServiceClient ));
Clientservice. Start ();
}
Catch (Exception ex)
{
MessageBox. Show ("listening Error:" + ex. Message );
}
}
}
Private void ServiceClient ()
{
Socket client = clientsocket;
Bool keepalive = true;


While (keepalive)
{
Byte [] buffer = new Byte [1024];
Int bufLen = 0;
Try
{
BufLen = client. Available;

Client. Receive (buffer, 0, bufLen, SocketFlags. None );
If (bufLen = 0)
Continue;
}
Catch (Exception ex)
{
MessageBox. Show ("Receive Error:" + ex. Message );
Return;
}

String clientcommand = System. Text. Encoding. ASCII. GetString (buffer). Substring (0, bufLen );

String [] tokens = clientcommand. Split (new Char [] {| });
Console. WriteLine (clientcommand );

If (tokens [0] = "CONN ")
{
For (int n = 0; n <clients. Count; n ++)
{
Client cl = (Client) clients [n];
SendToClient (cl, "JOIN |" + tokens [1]);
}
EndPoint ep = client. RemoteEndPoint;
Client c = new Client (tokens [1], ep, clientservice, client );

String message = "LIST |" + GetChatterList () + "";
SendToClient (c, message );

Clients. Add (c );


& N

Related Article

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.