Socket programming network chat

Source: Internet
Author: User
Tags getstream

/// Note: the following code is copied and executed in three console programs.

/// Step 1: Create a class library for serialization and pass

[System. serializable]
Public class filetest
{
Private string name;

Private int age;

Private string message;

Public String message
{
Get {return message ;}
Set {message = value ;}
}

Public int age
{
Get {return age ;}
Set {age = value ;}
}

Public string name
{
Get {return name ;}
Set {name = value ;}
}
}

// Step 2: server code

 

Class Program
{
Static void main ()
{
New Program (). gettarget ();
}

System. collections. generic. dictionary <string, system. net. sockets. tcpclient> DR = new system. collections. generic. dictionary <string, system. net. sockets. tcpclient> ();

// After obtaining the connection object, create a thread and execute the new connection (read and write)
Private void gettarget ()
{
System. net. sockets. tcplistener TL = new system. net. sockets. tcplistener (system. net. IPaddress. parse ("127.0.0.1"), 2009 );

TL. Start ();

While (true)
{
System. net. sockets. tcpclient Tc = tl. accepttcpclient ();
System. datetime dt = system. datetime. now;
String S = DT. tostring ("yymmddhhmmss") + dt. millisecond. tostring ();
Dr. Add (S, Tc );
System. Threading. Thread th = new system. Threading. Thread (getmessage );
Th. Name = s;
Th. isbackground = true;
Th. Start (TC );
System. Threading. thread. Sleep (1 );
}
}

Private void getmessage (Object O)
{
System. net. sockets. tcpclient Tc = (system. net. sockets. tcpclient) O;
System. net. sockets. networkstream NS = tc. getstream ();
System. runtime. serialization. iformatter formatter = new system. runtime. serialization. formatters. Binary. binaryformatter ();
Try
{
While (true)
{
Filetest Ft = (filetest) formatter. deserialize (NS );
System. Console. writeline (FT. Message );
Foreach (system. Collections. Generic. keyvaluepair <string, system. net. sockets. tcpclient> SCG in DR)
{
System. net. sockets. tcpclient TT = SCG. value;
System. net. sockets. networkstream NNS = TT. getstream ();
Formatter. serialize (NNS, ft );
}
}
}
Catch (system. Exception E)
{
Dr. Remove (system. Threading. thread. currentthread. Name );
System. Threading. thread. currentthread. Abort ();
}

}
}

// Step 3: client code

 

Class Program
{
Static void main ()
{
System. net. sockets. tcpclient Tc = new system. net. sockets. tcpclient ();
TC. Connect (system. net. IPaddress. parse ("127.0.0.1"), 2009 );
System. net. sockets. networkstream NS = tc. getstream ();
System. Threading. Thread th = new system. Threading. Thread (sendmessage );
Th. isbackground = true;
Th. Start (NS );
System. Threading. Thread Th1 = new system. Threading. Thread (getmessage );
Th. isbackground = true;
Th2.start (NS );
}

Private Static void sendmessage (Object O)
{
System. net. sockets. networkstream NS = (system. net. sockets. networkstream) O;
System. runtime. serialization. iformatter formatter = new system. runtime. serialization. formatters. Binary. binaryformatter ();
Try
{
While (true)
{
String S = system. Console. Readline ();
Filetest Ft = new filetest ();
Ft. Message = s;
Formatter. serialize (NS, ft );
}
}
Catch (system. Exception E)
{
System. Console. writeline ("error: {0}", E. Message );
}
}

Private Static void getmessage (Object O)
{
System. runtime. serialization. iformatter formatter = new system. runtime. serialization. formatters. Binary. binaryformatter ();
System. net. sockets. networkstream nw = (system. net. sockets. networkstream) O;
Try
{
While (true)
{
Filetest Ft = (filetest) formatter. deserialize (NW );
System. Console. writeline (FT. Message );
}
}
Catch (system. Exception E)
{
System. Console. writeline ("error: {0}", E. Message );
System. Console. Readline ();
System. environment. Exit (0 );
}

}

}

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.