View Single Post
  #7 (permalink)  
Old 10-03-2008, 08:49 PM
Norm's Avatar
Norm Norm is offline
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
Can you strip the problem down to a single file with client and server in the same file and a main that starts both of them?

Without seeing the code, I can't help. The code your link goes to has to much extra code that is not part of the problem.
Here is a sample client/server program that uses writeInt. Can you make it show your problem?
Code:
/* Changed to use DataStream - send first a byte code describing what follows */ //*************************************************************************** import java.net.*; import java.io.*; import javax.swing.*; import java.awt.*; import java.util.*; import java.awt.event.*; import java.awt.Window.*; import java.awt.Label; public class ClientServerTest2 { // extends Thread { final public static byte StringCode = 1; final public static byte IntCode = 2; //********************************************* // Starts here public static void main(String args[]){ Server2.main(new String[]{}); // start the server final ClientGUI2 cGUI = new ClientGUI2(); Runtime.getRuntime().addShutdownHook(new Thread(){ // @Override public void run() { // NOT called??? System.out.println("Shutting down"); cGUI.shutdown(); } }); } // end main() } // end class ClientServerTest //************************************************************************** class ClientGUI2 extends JFrame { Socket socket; DataInputStream in; DataOutputStream out; String host; int port = 1234; // String message; private JFrame frame; private JTextField TeF = new JTextField(25); JTextArea TeA = new JTextArea(20,30); JScrollPane scroll = new JScrollPane(TeA); JButton b1 = new JButton("SEND"); String fileName = " "; String savedFile = " "; JPanel west = new JPanel(); JPanel east = new JPanel(); JPanel south = new JPanel(); MyJButtonListener2 myJButtonListener; final public static String ShutDownMsg = "ShuttingDown"; private void setupGUI() { TeA.setEditable(false); // output only TeA.setBackground(Color.lightGray); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(BorderLayout.EAST,east); west.add(scroll); c.add(BorderLayout.WEST,west); south.add(TeF); south.add(b1); c.add(BorderLayout.SOUTH,south); myJButtonListener = new MyJButtonListener2(this); //<<<<<<<<<<< b1.addActionListener(myJButtonListener); setSize(400,380); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //---------------------------------------- // Constructor ClientGUI2(){ super("Client - Server test - DataStreams"); setupGUI(); getConnection(); receiveMessage(); } public void getConnection() { try { host = InetAddress.getLocalHost().getHostAddress(); socket = new Socket(host,port); in = new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); // System.out.println("Client - getConn out=" + out); }catch(UnknownHostException e){ System.out.println("Client - getConnection():" + e); }catch(IOException e) { System.out.println("Client - getConnection():" + e); } } final static int NoValue = -1; public void sendMessage(String message) { // System.out.println("out= " + out); // Is message a number or text? int value = NoValue; // To hold int value if found try { value = Integer.parseInt(message); // test/get int value }catch(NumberFormatException nfe){ } try { // Use different protocol for String vs int if(value == NoValue) { out.writeByte(ClientServerTest2.StringCode); // code out.writeByte(message.length()); // length out.writeChars(message); // data }else { out.writeByte(ClientServerTest2.IntCode); // code out.writeInt(value); // value } System.out.println("Client sent:- " + message); }catch(IOException x) { System.out.println("Client got ex"); x.printStackTrace(); } } public void receiveMessage() { String message = null; try { while (true){ byte code = in.readByte(); System.out.println("C-code=" + code); switch (code) { case ClientServerTest2.StringCode: int len = in.readByte(); StringBuffer sb = new StringBuffer(len); for(int i=0; i < len; i++) { char c = in.readChar(); sb.append(c); } message = sb.toString(); break; case ClientServerTest2.IntCode: break; default: message = "C-UNKNOWN CODE " + code; System.out.println(message); } //end switch System.out.println("Client Received:" + message); // Show message and clear TeA.append(message + "\n"); TeF.setText( ""); // clear input area TeF.requestFocus(); } // end while() }catch(IOException e) { System.out.println("Client - receiveMessage():" + e); } } public void shutdown() { sendMessage(ShutDownMsg); } //--------------------------------------------- class MyJButtonListener2 implements ActionListener{ ClientGUI2 clientgui; MyJButtonListener2(ClientGUI2 cg) { clientgui = cg; // save } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("SEND")) { clientgui.sendMessage(TeF.getText()); }else{ System.out.println("unknown ae=" + e); } } } } // end class ClientGUI2 //*************************************************************************** class Server2 extends Thread { ServerSocket server; Socket connection; DataInputStream in; DataOutputStream out; int port = 1234; // String message; public static void main(String args[]) { Server2 server = new Server2(); server.start(); } public void run() { getConnection(); recieveMessage(); } private void getConnection() { try { server = new ServerSocket(port); System.out.println("Waiting for client..."); connection = server.accept(); System.out.println("Server got conection: " + connection); in = new DataInputStream(connection.getInputStream()); out = new DataOutputStream(connection.getOutputStream()); } catch(IOException e) { System.out.println("Server - getConnection();" + e); } } private void recieveMessage() { try { String message = null; while(true) { byte code = in.readByte(); System.out.println("S-code=" + code); switch (code) { case ClientServerTest2.StringCode: int len = in.readByte(); StringBuffer sb = new StringBuffer(len); for(int i=0; i < len; i++) { char c = in.readChar(); sb.append(c); } message = sb.toString(); break; case ClientServerTest2.IntCode: int value = in.readInt(); message = "int("+value+")"; break; default: message = "S-unknown code: " + code; System.out.println(message); } //end switch System.out.println("Server - recMsg msg=" + message); sendMessage("Returned:" + message); // send it back } // end while() } catch(IOException e) { System.out.println("Server -receiveMessage():" + e); } } private void sendMessage(String message){ try{ out.writeByte(ClientServerTest2.StringCode); // code out.writeByte(message.length()); // length out.writeChars(message); // data }catch(IOException x) { System.out.println("Server got ex"); x.printStackTrace(); } } } // end class Server
Reply With Quote