Java chat client does not display the content, the server is not the cause of the problem finally found

Source: Internet
Author: User
Tags try catch

O (≧ mouth ≦) o just hit a lot of content, the results forget the network disconnected, unable to automatically save the draft. The results of the release of the contents have been lost. The air died, and did not write.

Today is about the Java EE, speak quickly, mainly on the focus or is not the same as SE, just a bit, almost jumped over, let us see ppt ... (content not dropped) ...

I made a simple chat room function code last week, and always went wrong. The content of the hair can only be seen on the server, but the client cannot see it. The string waits to be received, but it is not received. The code of the teacher was changed several times, or not. Also let the teacher help look for a long time, also still did not find the problem. This afternoon again tried to do again, but the code is not ripe, or according to their previous code written, old problems. The evening looked at the teacher's code to learn again, finally good, and then found out where the problem.

It turns out that there is no other way for the server to traverse the write content, and the server reads the content in a try Catch statement, and the result has been unable to wait there.

This blog post, change the title, I hope if there is the same problem can be seen. Original title: javase_day_024


Stick to the code of the teacher, I almost, will not post.

First, you have to run the service side first.

Package Com.xinbo;


Import Java.awt.BorderLayout;

Import Java.io.DataInputStream;

Import Java.io.DataOutputStream;

Import java.io.IOException;

Import Java.net.ServerSocket;

Import Java.net.Socket;

Import java.util.ArrayList;

Import Java.util.concurrent.ExecutorService;

Import java.util.concurrent.Executors;

Import Javax.swing.JFrame;

Import Javax.swing.JScrollPane;

Import Javax.swing.JTextArea;


public class TestServer {

private static JTextArea jta=new JTextArea ();

private static ServerSocket server;

private static arraylist<socket> list=new arraylist<socket> ();//Receive Client information

private static executorservice exec;

Static class Talk implements Runnable{

Private Socket client;

Private DataInputStream fromclient;

Private DataOutputStream toclient;

String msg;

Public talk (Socket client) {

This.client=client;

try {

Fromclient=new DataInputStream (Client.getinputstream ());

} catch (IOException e) {

}

}


@Override

public void Run () {

try {

while ((Msg=fromclient.readutf ())!=null) {

Msg= "[" +client.getport () + "] said:" +MSG;

Jta.append (msg+ "\ n");

Xiaomessage ();

}

} catch (IOException e) {

}

}

public void Xiaomessage () {

for (Socket client:list) {

try {

Toclient=new DataOutputStream (Client.getoutputstream ());

Toclient.writeutf (msg);

} catch (IOException e) {

}

}

}

}


public static void Main (string[] args) {

JFrame f=new JFrame ("service Side");

F.add (New JScrollPane (JTA), borderlayout.center);

F.setbounds (100, 100, 400, 300);

F.setdefaultcloseoperation (Jframe.exit_on_close);

F.setvisible (TRUE);

try {

Server=new ServerSocket (1314);

Exec=executors.newcachedthreadpool ();

Jta.append ("Server is turned on");

Socket client =null;

while (true) {

Client=server.accept ();

List.add (client);

Exec.execute (New Talk (client));

}

} catch (IOException e) {

}


}


}

Then run the client, you can open several windows.

Package Com.xinbo;


Import Java.awt.BorderLayout;

Import java.awt.event.ActionEvent;

Import Java.awt.event.ActionListener;

Import Java.io.DataInputStream;

Import Java.io.DataOutputStream;

Import java.io.IOException;

Import Java.net.Socket;

Import java.net.UnknownHostException;


Import Javax.swing.JButton;

Import Javax.swing.JFrame;

Import Javax.swing.JPanel;

Import Javax.swing.JTextArea;

Import Javax.swing.JTextField;


public class TestClient {

private static JPanel p=new JPanel ();

private static JTextField jtf=new JTextField ();

private static JTextArea jta=new JTextArea ();

private static DataInputStream fromserver;

private static DataOutputStream Toserver;

private static JButton jb=new JButton ("send");


public static void Main (string[] args) {

JFrame f=new JFrame ("client");

P.setlayout (New BorderLayout ());

JPanel p2=new JPanel ();

P2.setlayout (New BorderLayout ());

P2.add (Jtf,borderlayout.center);

P2.add (Jb,borderlayout.east);

P.add (P2,borderlayout.south);

P.add (Jta,borderlayout.center);

F.add (P);

F.setvisible (TRUE);

F.setdefaultcloseoperation (Jframe.exit_on_close);

F.setbounds (100, 100, 400, 300);

Jb.addactionlistener (new ActionListener () {

@Override

public void actionperformed (ActionEvent e) {

String Str=jtf.gettext (). toString ();

try {

Toserver.writeutf (str);

Toserver.flush ();

} catch (IOException E1) {

}finally{

Jtf.settext ("");

}

}

});

Socket client;

try {

Client=new Socket ("localhost", 1314);

Fromserver=new DataInputStream (Client.getinputstream ());

Toserver=new DataOutputStream (Client.getoutputstream ());

while (true) {

String Msg=fromserver.readutf ();

if (msg!=null) {

Jta.append (msg+ "\ n");

}

}

} catch (Unknownhostexception E1) {

} catch (IOException E1) {

}

}


}









Java chat client does not display the content, the server is not the cause of the problem finally found

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.