Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search Coderanch
Advance search
Google search
Register / Login
Huynh Tuan
Greenhorn
+ Follow
1
Posts
1
Threads
0
Cows
since Jun 26, 2012
Merit badge:
grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
Ranch Hand Scavenger Hunt
Number Posts (1/100)
Number Threads Started (1/100)
Number Cows Received (0/5)
Number Likes Received (0/10)
Number Likes Granted (0/20)
Set bumper stickers in profile (0/3)
Report a post to the moderators (0/1)
Edit a wiki page (0/1)
Create a post with an image (0/2)
Greenhorn Scavenger Hunt
First Post
Number Posts (1/10)
Number Threads Started (1/10)
Number Likes Received (0/3)
Number Likes Granted (0/3)
Set bumper stickers in profile (0/1)
Set signature in profile
Search for a post/thread (0/3)
Set a watch on a thread
Save thread as a bookmark
Create a post with an image (0/1)
Recent posts by Huynh Tuan
Problem append JtextArea
I can use System.out.println().But I can't append text after receiving from the client.
package ServerSt; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.*; import java.net.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import static java.lang.System.out; @SuppressWarnings("serial") public class ServerSt extends JFrame implements ActionListener{ DataOutputStream dos; DataInputStream dis; Vector<String> users = new Vector<String>(); Vector<HandleClient> clients = new Vector<HandleClient>(); //Socket soc; //UI JPanel pane=new JPanel(); JTextArea txt=new JTextArea(10,10); JScrollPane src=new JScrollPane(txt); JButton btns=new JButton("Start"); JButton btnst=new JButton("Stop"); JTextField txtf=new JTextField(10); public ServerSt() { // TODO Auto-generated constructor stub setLayout(new GridLayout(10,1)); setSize(400,400); pane.add(btns); pane.add(btnst); add(pane); this.add(src); add(txtf); setVisible(true); btns.addActionListener(this); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==btns){ try { process(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } String name; public void process() throws Exception { ServerSocket server = new ServerSocket(9999,10); while( true) { Socket client = server.accept(); dos=new DataOutputStream(client.getOutputStream()); HandleClient c = new HandleClient(client); dos.writeUTF("server start"); dos.flush(); txtf.setText("asdjslkdjs"); //dis=new DataInputStream(client.getInputStream()); clients.add(c); //name=dis.readUTF(); //txt.append(name+" signed"); } // end of while } public void AppenT(String s) { txt.append(s+"\n"); } public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub new ServerSt(); } public void boradcast(String user, String message) { // send message to all connected users for ( HandleClient c : clients ) //c.sendMessage("server","hi"); if ( ! c.getUserName().equals(user) ){ c.sendMessage(user,message); } //txt.append(user+":"+message); } class HandleClient extends Thread { String name = ""; //BufferedReader input; DataInputStream input; //PrintWriter output; DataOutputStream output; public HandleClient(Socket client) throws Exception { // get input and output streams //input = new BufferedReader( new InputStreamReader( client.getInputStream())) ; input=new DataInputStream(client.getInputStream()); //output = new PrintWriter ( client.getOutputStream(),true); output=new DataOutputStream(client.getOutputStream()); // read name //name = input.readLine(); name=input.readUTF(); //JOptionPane.showMessageDialog(null, name); //sleep(1000); //txt.append(name); users.add(name); // add to vector System.out.println(name+" signed"); //txt.append(name+" signed"); //AppenT(name+" signed"); //txt.append(name); //txt.append(name+" signed"); start(); } public void sendMessage(String uname,String msg) { try { output.writeUTF(uname + ":" + msg); output.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getUserName() { return name; } public void run() { String line; try { while(true) { line=input.readUTF(); if ( line.equals("end") ) { clients.remove(this); users.remove(name); break; } boradcast(name,line); // method of outer class - send messages to all //txt.append(name+":"+line); } // end of while } // try catch(Exception ex) { System.out.println(ex.getMessage()); } } // end of run() } // end of inner class } //
show more
11 years ago
Sockets and Internet Protocols