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.
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);
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();
}
}
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 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);
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();
}
}
01-03-2007, 05:59 AM
sudheerprem
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());
01-07-2007, 11:54 PM
Rmstn1580
Thanks a lot! It worked. I can't believe I didn't think of that O_o