Java socket console version chat room program source code download, socket source code download

Source: Internet
Author: User

Java socket console version chat room program source code download, socket source code download

Original article: java socket console version chat room program source code download

Code: http://www.zuidaima.com/share/1550463257578496.htm

Java socket console version chat room program source code download, written when learning, suitable for learning java basics java Network Programming Basics


Tags: java socket console chat room source code topic: Network Programming


High-score urgent Java chat room Socket source code (Lite version can be run most importantly)

This should be able to pull:

Import java. applet .*;
Import java. awt .*;
Import java. io .*;
Import java.net .*;
Import java. awt. event .*;
Public class ChatClient extends Applet {
Protected boolean loggedIn; // logon status
Protected Frame cp; // chat room framework
Protected static int PORTNUM = 7777; // default port number: 7777
Protected int port; // the actual port number.
Protected Socket sock;
Protected BufferedReader is; // BufferedReader used to read data from sock
Protected PrintWriter pw; // The PrintWriter used to write data to sock
Protected TextField tf; // The TextField used for Input
Protected TextArea ta; // TextArea used to display the conversation
Protected Button lib; // the login Button
Protected Button lob; // The logout Button.
Final static String TITLE = "Chatroom applet >>>>>>>>>>>>>>>>>>>>> ";
Protected String paintMessage; // published message
Public ChatParameter Chat;
Public void init (){
PaintMessage = "generating chat window ";
Repaint ();
Cp = new Frame (TITLE );
Cp. setLayout (new BorderLayout ());
String portNum = getParameter ("port"); // do not specify parameters.
Port = PORTNUM;
If (portNum! = Null) // The book is portNum = null, which is very problematic.
Port = Integer. parseInt (portNum );
// CGI
Ta = new TextArea (14,80 );
Ta. setEditable (false); // read only attribute
Ta. setFont (new Font ("Monospaced", Font. PLAIN, 11 ));
Cp. add (BorderLayout. NORTH, ta );
Panel p = new Panel ();
Button B;
// For login button
P. add (lib = new Button ("Login "));
Lib. setEnabled (true );
Lib. requestFo... the remaining full text>

A chat room program compiled using java socket can run the accompanying source code. Please send it together with a tutorial. Email: hogsdi112 @ 163com

Import java.net .*;
Import java. io .*;
Import java. util .*;
Public class ChatRoomServer {
Public static void main (String [] args) throws Exception {
ServerSocket ss = new ServerSocket (8000 );
List sockets = new ArrayList ();
While (true ){
Socket s = ss. accept ();
Sockets. add (s );
Thread t = new ServerThread1 (s, sockets );
T. start ();
}
}
}
Class ServerThread1 extends Thread {
Socket s;
List sockets;
Public ServerThread1 (Socket s, List sockets ){
Super ();
This. s = s;
This. sockets = sockets;
}
Public void run (){
Try {
BufferedReader in = new BufferedReader (
New InputStreamReader (s. getInputStream ()));
While (true ){
String text = in. readLine ();
If (text = null) return;
For (int I = 0; I <sockets. size (); I ++ ){
Socket s2 = (Socket) sockets. get (I );
PrintWriter out = new PrintWriter (s2.getOutputStream ());
Out. println (text );
Out. flush ();
}

}
} Catch (IOException e ){}
Finally {
This. sockets. remove (s );
}
}
}

Import javax. swing .*;
Import java. awt. event .*;
Import java.net .*;
Import java. io .*;
Public class ChatRoomClient {
JFrame;
JTextField jtf;
JTextArea jta;
Socket s;
BufferedReader in;
PrintWriter out;

Public static void main (String [] args ){
ChatRoomClient c = new ChatRoomClient ();
C. receive ();
}

Public ChatRoomClient (){
Frame = new JFrame ("Chat Room ");
Frame. setSize (400,300 );
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );

Jta = new JTextArea ();
JScrollPane jsp = new JScrollPane ...... remaining full text>

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.