Byte Problems


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Byte Problems

  1. #1
    Join Date
    Dec 2006
    Posts
    6

    Byte Problems

    I'm attempting to create a program that acts as a server, and when it gets connections from a client, it sends the text and the client opens a new window and displays the text. I got it to connect and send and everything, but the text says "84" instead of what it's supposed to say. I don't know if that's a problem with the bytes or what. Well, here's my code. I've taken out a few of the non-needed classes.

    Server code
    Code:
    package halturnerserver;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.lang.Thread.*;
    import java.io.*;
    
    class Window extends JFrame {    
        Window() {
            super("Server Control");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            Content x = new Content();
            
            add(x);
            
            pack();
            setVisible(true);
        }
    }
    
    class Content extends JPanel implements ActionListener {
        JPanel row1 = new JPanel();
        JPanel row2 = new JPanel();
        JLabel status = new JLabel("Disconnected");
        JButton connect = new JButton("Connect");
        JButton disconnect = new JButton("Disconnect");
        
        Content() {
            GridLayout gl = new GridLayout(1, 2, 1, 1);
            FlowLayout center = new FlowLayout(FlowLayout.CENTER);
            setLayout(gl);
            
            row1.setLayout(center);
            row2.setLayout(center);
            
            connect.setEnabled(true);
            disconnect.setEnabled(false);
            
            connect.addActionListener(this);
            disconnect.addActionListener(this);
            
            row1.add(status);
            row2.add(connect);
            row2.add(disconnect);
            
            add(row1);
            add(row2);
        }
    
        public void actionPerformed(ActionEvent evt) {
            Object o = evt.getSource();
            Server s = new Server();
            try {
                if(o == connect) {
                    connect.setEnabled(false);
                    disconnect.setEnabled(true);
                    status.setText("Connected");
                    s.start();
                    s.setPriority(10);
                }
                if(o == disconnect) {
                    connect.setEnabled(true);
                    disconnect.setEnabled(false);
                    status.setText("Disconnected");
                    s.join();
                }
            }catch(InterruptedException e) {
                Error x = new Error(e.toString());
            }
        }
    }
    
    class Server extends Thread {
        public void run() {
            while(true) {
                try {
                    int port = 4321;
                    ServerSocket ss = new ServerSocket(port);
                    while(true) {
                        Socket s = ss.accept();
                        
                        OutputStream os = s.getOutputStream();
                        DataOutputStream dos = new DataOutputStream(os);
                        dos.writeBytes("This is a test");
                        
                        s.close();
                    }
                }
                catch(Exception e) {
                Error x = new Error(e.toString());
                }
            }
        }
    }
    
    class Error extends JFrame implements ActionListener {
        JButton ok = new JButton(" Ok ");
        Error(String text) {
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            
            JPanel row1 = new JPanel();
            JPanel row2 = new JPanel();
            JLabel message = new JLabel(text);
            
            GridLayout gl = new GridLayout(1, 2, 1, 1);
            setLayout(gl);
            FlowLayout center = new FlowLayout(FlowLayout.CENTER);
            
            row1.setLayout(center);
            row2.setLayout(center);
            
            ok.addActionListener(this);
            
            row1.add(message);
            row2.add(ok);
            
            add(row1);
            add(row2);
            
            pack();
            setVisible(true);
        }
        
        public void actionPerformed(ActionEvent evt) {
            Object o = evt.getSource();
            if(o == ok) {
                setVisible(false);
            }
        }
    }
    
    class Main {
        public static void main(String args[]) {
            Window x = new Window();
        }
    }
    Client code
    Code:
    package halturnerclient;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.lang.Thread.*;
    import javax.swing.ImageIcon.*;
    import java.io.*;
    
    class Error extends JFrame implements ActionListener {
        JButton ok = new JButton(" Ok ");
        Error(String text) {
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            
            JPanel row1 = new JPanel();
            JPanel row2 = new JPanel();
            JLabel message = new JLabel(text);
            
            GridLayout gl = new GridLayout(2, 1, 1, 1);
            setLayout(gl);
            FlowLayout center = new FlowLayout(FlowLayout.CENTER);
            
            row1.setLayout(center);
            row2.setLayout(center);
            
            ok.addActionListener(this);
            
            row1.add(message);
            row2.add(ok);
            
            add(row1);
            add(row2);
            
            pack();
            setVisible(true);
        }
        
        public void actionPerformed(ActionEvent evt) {
            Object o = evt.getSource();
            if(o == ok) {
                setVisible(false);
            }
        }
    }
    
    class Window extends JFrame {
        Window() {
            super("Hal Turner Show");
            setSize(300, 150);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            Content x = new Content();
            
            add(x);
            
            setVisible(true);
        }
    }
    
    class Content extends JPanel implements ActionListener {
        Font titleFont = new Font("Dialog", Font.BOLD, 16);
        JPanel infoRow = new JPanel();
        JPanel buttonRow = new JPanel();
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("File");
        JMenuItem about = new JMenuItem("About");
        JMenuItem help = new JMenuItem("Help");
        JButton connect = new JButton("Receive Data");
        JLabel message = new JLabel("No data has been sent for");
        
        Content() {
            BorderLayout bl = new BorderLayout();
            setLayout(bl);
            FlowLayout center = new FlowLayout(FlowLayout.CENTER);
            FlowLayout left = new FlowLayout(FlowLayout.LEFT);
            
            infoRow.setLayout(left);
            buttonRow.setLayout(center);
            
            connect.addActionListener(this);
            
            infoRow.add(message);
            buttonRow.add(connect);
            
            help.addActionListener(this);
            about.addActionListener(this);
            menu.add(help);
            menu.add(about);
            menuBar.add(menu);
            
            add(menuBar, BorderLayout.NORTH);
            add(infoRow, BorderLayout.SOUTH);
            add(buttonRow, BorderLayout.WEST);
            
            setVisible(true);
        }
        
        public void actionPerformed(ActionEvent evt) {
            Object o = evt.getSource();
            Client c = new Client();
            try {
                if(o == connect) {
                    message.setText("Data has been sent for");
                    c.start();
                }
                if(o == about) {
                    About a = new About();
                }
                if(o == help) {
                    Help h = new Help();
                }
            }catch(Exception e) {
                Error x = new Error(e.toString());
            }finally {
                try {
                    c.join();
                }catch(Exception e) {
                    Error er = new Error(e.toString());
                }
            }
        }
    }
    
    class Client extends Thread {
        public void run() {
            try {
                String server = "127.0.0.1";
                int port = 4321;
                
                Socket s = new Socket(server, port);
                
                InputStream is = s.getInputStream();
                DataInputStream dis = new DataInputStream(is);
                byte i = dis.readByte();
                Byte b = new Byte(i);
                
                Info info = new Info(b.toString());
                
                s.close();
            }catch(Exception e) {
                Error x = new Error(e.toString());
            }
        }
    }
    
    class Info extends JFrame {
        Info(String text) {
            super("Received Text from Server");
            setSize(300, 300);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            
            JPanel row = new JPanel();
            JLabel dataArea = new JLabel(text);
            
            FlowLayout center = new FlowLayout(FlowLayout.CENTER);
            setLayout(center);
            row.setLayout(center);
            
            row.add(dataArea);
            
            add(row);
            
            setVisible(true);
        }
    }
    
    class Main {
        public static void main(String args[]) {
            Window x = new Window();
        }
    }

  2. #2
    Join Date
    Dec 2006
    Location
    Kerala,India
    Posts
    51
    the problem is with the following code..
    Code:
    byte i = dis.readByte();
    Byte b = new Byte(i);
    Byte.toString() method will convert the the byte value to String. Also readByte() will read only one byte.You can do the following
    Code:
    byte b [] = new byte[1024] //read one kb
    int i = 0;
    StringBuffer sb = new StringBuffer();
    while ((i=dis.read(b,0,1024))!=-1) {
    sb.append(new String(b,0,i));
    }
    Info info = new Info(sb.toString());

  3. #3
    Join Date
    Dec 2006
    Posts
    6
    Thanks a lot! It worked. I can't believe I didn't think of that O_o

Similar Threads

  1. Replies: 3
    Last Post: 09-12-2002, 08:25 PM
  2. Replies: 2
    Last Post: 06-18-2002, 10:15 AM
  3. Replies: 0
    Last Post: 04-04-2002, 07:46 PM
  4. Problems with NetAddUser
    By Jeff Morgan in forum VB Classic
    Replies: 2
    Last Post: 02-13-2001, 11:49 AM
  5. Problems with NetAddUser
    By Jeff Morgan in forum VB Classic
    Replies: 0
    Last Post: 02-11-2001, 01:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center