2 Replies - 2888 Views - Last Post: 11 November 2012 - 12:45 PM Rate Topic: *---- 1 Votes

#1 Shamam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-November 12

TCP client/server the server works properly error in the client

Posted 10 November 2012 - 01:22 PM

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author amer
 */
public class MultiEchoClientGUIver2 extends JFrame implements ActionListener{
    private JLabel prompt;
    private JTextField hostInput;
private JTextArea display;
private JButton messageButton;
private JButton exitButton;
private JPanel buttonPanel;
private JPanel hostpanal;
private static Socket socket ;
 private static InetAddress host;
 private static String host2="192.168.0.1";

public static void main(String[] args) 
{
   
    try {
            
            host=InetAddress.getLocalHost();
        } 
        catch (UnknownHostException ex) {
            
        }
   
    MultiEchoClientGUIver2 frame = new MultiEchoClientGUIver2();
frame.setSize(400,300);
frame.setVisible(true);
frame.addWindowListener(
new WindowAdapter()
{
public void windowClosing(
WindowEvent event)
{
//Check whether a socket is open...
if (socket != null)
{
try
{
socket.close();
}
catch (IOException ioEx)
{
System.out.println(
"\nUnable to close link!\n");
System.exit(1);
}
}
System.exit(0);
}
}
);


}
    
    
public MultiEchoClientGUIver2()
{
    hostpanal =new JPanel();
    prompt=new JLabel("Enter Message: ");
hostInput = new JTextField(20);
hostpanal.add(prompt);
hostpanal.add(hostInput);
add(hostpanal, BorderLayout.NORTH);
display = new JTextArea(10,15);
//Following two lines ensure that word-wrapping
//occurs within the JTextArea...
display.setWrapStyleWord(true);
display.setLineWrap(true);
add(new JScrollPane(display),
BorderLayout.CENTER);
buttonPanel = new JPanel();
messageButton = new JButton("send message ");
messageButton.addActionListener(this);
buttonPanel.add(messageButton);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);
add(buttonPanel,BorderLayout.SOUTH);
}
 private static final int PORT = 1234;
 


  
   
public void actionPerformed(ActionEvent event)
{   
    if (event.getSource() == exitButton)
   
System.exit(0);  
             
  try{
        
         socket=new Socket(host,PORT);
          Scanner Input =new Scanner(socket.getInputStream());
        PrintWriter output =new PrintWriter(socket.getOutputStream(),true);

   

String message,response;
        do{
              message=hostInput.getText();
 

    output.println(message);
      response = Input.nextLine();

        display.append("\nSERVER> " + response );
        hostInput.setText("");

     
             
        }while(!message.equals("QUIT"));}
catch(IOException ioEx){
}

 finally
{
try
{
System.out.println(
"\nClosing connection...");
socket.close();
}
catch(IOException ioEx)
{
System.out.println(
"Unable to disconnect!");
System.exit(1);
}
}  
}}



the error in the action performed method but i dont know what is the error!!

Is This A Good Question/Topic? 0
  • +

Replies To: TCP client/server the server works properly error in the client

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3465
  • View blog
  • Posts: 15,680
  • Joined: 20-September 08

Re: TCP client/server the server works properly error in the client

Posted 11 November 2012 - 10:52 AM

You can't run networking code in an ActionListener. Swing is single-threaded so your code will simply block the proper processing of windowing events. Unless your 'hostInput' already says QUIT, you're unlikely ever to get it to.
Look at the documentation for SwingWorker to see how to handle mixed application and gui code in terms of threads.
Also, NEVER ignore exceptions unless you know exactly what you're doing
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: TCP client/server the server works properly error in the client

Posted 11 November 2012 - 12:45 PM

public static void main(String[] args) 
{
   
    try {
            
            host=InetAddress.getLocalHost();
        } 
        catch (UnknownHostException ex) {
            // so what do you do here ?   
            // you can't simply continue         
        }
   
    MultiEchoClientGUIver2 frame = new MultiEchoClientGUIver2();
frame.setSize(400,300);
frame.setVisible(true);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1