basic socket server in unstable

R

Robot

Hi, I have to use 1.4.1_03 version for development and I am running
into a really weird problem.
I have a server-client program, that receives data from a client and
sends it back to it.

The problem is that, for some reason, this is not what is happening.
It works fine when I only have only one client, but when I have more
than one, the whole hell breaks loose. Can someone please tell me
where I went wrong (other than applying for this job?)

import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class sv0 {
private static final int PORT = 55555;
private static ServerSocket ss = null;
private static Hashtable connectInfo = null;

public static void main(String[] args) {

try {
ss = new ServerSocket(PORT);
while(true) {
Socket s = ss.accept();
Thread t = new Thread(new DoClient(s));
s = null;
t.start();
}
}catch(IOException e) {
System.out.println("main(IOException):" + e);
}
}
}

// client thread
class DoClient implements Runnable, svIF {
private static Socket s = null; //
private static ObjectInputStream in = null; //
private static ObjectOutputStream out = null; //
private static String ThisDN = null; //

public DoClient(Socket s) throws IOException {
System.out.println("---------------Thread
running----------------(DoClient)");
this.s = s;
in = new ObjectInputStream(s.getInputStream());
out = new ObjectOutputStream(s.getOutputStream());
};

public void run() {
MyData d = new MyData();
try {
boolean flag = true;
while(flag) {
String str = null;
try {
d = (MyData)in.readObject();
out.writeObject(d);
out.flush();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
s.close();
}catch(IOException e) {
}finally {
try {
s.close();
} catch (IOException ex) {
}
}
}
}
class MyData implements Serializable {
String a;
int b;
}

------

Client source code:

import java.awt.Color;
import java.lang.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class cl0 extends javax.swing.JFrame implements svIF {
static final String FNAME1 = "c:\\temp\\config1.txt";
static final String FNAME2 = "c:\\temp\\config2.txt";
static final String FNAME3 = "c:\\temp\\config3.txt";
static Socket s = null;
static ObjectInputStream in = null;
static ObjectOutputStream out = null;

static String ip = null;
static int port = 0;
static String name = null;

public cl0() {
initComponents();
}

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//
GEN-BEGIN:initComponents
private void initComponents() {
JbtnsendCmd1 = new javax.swing.JButton();
JLlogin = new javax.swing.JLabel();
JtxtMsg = new javax.swing.JTextField();
JbtnClose = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
JLname = new javax.swing.JLabel();
JBlogin = new javax.swing.JButton();

getContentPane().setLayout(new java.awt.GridLayout());


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
JbtnsendCmd1.setText("sendCmd1");
JbtnsendCmd1.addMouseListener(new
java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
JbtnsendCmd1MouseClicked(evt);
}
});
JbtnsendCmd1.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
JbtnsendCmd1ActionPerformed(evt);
}
});

getContentPane().add(JbtnsendCmd1);


JLlogin.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
JLlogin.setText("login");

JLlogin.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
getContentPane().add(JLlogin);

JtxtMsg.setText("test");
getContentPane().add(JtxtMsg);

JbtnClose.setText("Close");
JbtnClose.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
JbtnCloseActionPerformed(evt);
}
});

getContentPane().add(JbtnClose);

getContentPane().add(jLabel1);

JLname.setText("jLabel2");
getContentPane().add(JLname);

JBlogin.setText("login");
JBlogin.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
JBloginMouseClicked(evt);
}
});

getContentPane().add(JBlogin);

pack();
}// </editor-fold>//GEN-END:initComponents

private void JBloginMouseClicked(java.awt.event.MouseEvent evt) {//
GEN-FIRST:event_JBloginMouseClicked
try {
MyData d = new MyData();
d.a = name;
d.b=0;
out.writeObject(d);
out.flush();
}catch(Exception e) {
System.out.println(e);
}
}//GEN-LAST:event_JBloginMouseClicked

private void JbtnsendCmd1MouseClicked(java.awt.event.MouseEvent
evt) {//GEN-FIRST:event_JbtnsendCmd1MouseClicked
JOptionPane.showMessageDialog(getContentPane(), "cmd1");
try {
MyData d = new MyData();
d.a = "cmd1-cmd1";
d.b=0;
out.writeObject(d);
out.flush();
System.out.println("cmd1");
}catch(Exception e) {

}
}//GEN-LAST:event_JbtnsendCmd1MouseClicked

private void JbtnCloseActionPerformed(java.awt.event.ActionEvent
evt) {//GEN-FIRST:event_JbtnCloseActionPerformed
try {
s.close();
} catch (Exception e) {
}
System.exit(0);
}//GEN-LAST:event_JbtnCloseActionPerformed

private void
JbtnsendCmd1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_JbtnsendCmd1ActionPerformed

}//GEN-LAST:event_JbtnsendCmd1ActionPerformed


public static void main(String args[]) {
cl0 mainFrame = new cl0();
mainFrame.setVisible(true);

init(args[0]);

Thread t = new Thread(new DoClient());
t.start();
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton JBlogin;
public static javax.swing.JLabel JLlogin;
public static javax.swing.JLabel JLname;
public static javax.swing.JButton JbtnClose;
public static javax.swing.JButton JbtnsendCmd1;
public static javax.swing.JTextField JtxtMsg;
private javax.swing.JLabel jLabel1;
// End of variables declaration//GEN-END:variables
public static void setJLlogin() {
JLlogin.setForeground(Color.BLUE);
}
public static void setJtxt(String msg) {
JtxtMsg.setText("");
JtxtMsg.setText(msg);
}
public static void setJLname(String name) {
JLname.setText(name);
}
//****************************************************
//****************************************************
public static void init(String file_select) {
String file = null;

if (file_select.equals("1")) {
file = FNAME1;
}else if (file_select.equals("2")){
file = FNAME2;
}else {
file = FNAME3;
}
if (readParam(file) == false) {
return;
}
if (connectSv() == false) { return; }
}
private static boolean readParam(String fname) {

try {
String buff;
File f = new File(fname);

BufferedReader br = new BufferedReader(new FileReader(f));
while ((buff = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer(buff, ",");
name = st.nextToken(); //USER NAME
ip = st.nextToken(); //SERVER IP
port = Integer.parseInt(st.nextToken()); //SERVER PORT
}
//clFrame.setJLname(name);

return true;
}catch(Exception e) {
System.out.println(e);
e.printStackTrace();
return false;
}
}
private static boolean connectSv() {
try {
s = new Socket(ip, port);
cl0.out = new ObjectOutputStream(s.getOutputStream());
cl0.in = new ObjectInputStream(s.getInputStream());


return true;
}catch(Exception e) {
try {
s.close();
}catch(IOException e2) {
System.out.println(e2);
}
System.out.println(e);
e.printStackTrace();
return false;
}
}
}
//****************************************************
//Thread
//****************************************************
class DoClient implements Runnable {
MyData d = null;
public void run() {

while(cl0.s != null) {
String str = null;
try {
d = (MyData)cl0.in.readObject();
cl0.setJtxt(d.a);
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}

}
}
}
class MyData implements Serializable {
String a;
int b;
}

Thanks!
Thanks!
 
A

Andrew Thompson

Robot wrote:
...
It works fine when I only have only one client, but when I have more
than one, the whole hell breaks loose. Can someone please tell me
where I went wrong...

Why does this code sample show 2 MyData classes (OK -
they seem identical, so I am guessing we can put that
down to over zealous pasting) but more importantly, two
DoClient classes (which are most definitely not identical).

Note also that it is valuable for all code to have meaningful
class, attribute and method names that follow the common
nomenclature, and 'sv0' sure does not!

All this might be put down to 'a quick over simplification'
of the actual code. It is generally more useful to post
an SSCCE* that people can copy/paste/compile/run-and-
see-fail. Some extra tips the SSCCE document mentions,
are to make sure the code lines are short (I had to re-join
some lines before I could get it close to compiling, but
class 'svIF' seemed to be missing - so I could not take
it further).

* <http://www.physci.org/codes/sscce.html>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
A

Andrew Thompson

R

Robot

thank you very much for your prompt reply.

The code I have posted is actually two files - SERVER and CLIENT.

I have marked client with :

"Client source code"

Sorry if it was not too clear!!!!

But I am really flabberghasted why it is not working!
 
A

Andrew Thompson

Robot wrote:
..
The code I have posted is actually two files - SERVER and CLIENT.

Yes. I noted that. But I also noted that since the classes
were all in the 'default' package, if they resided in the same
directory at time of compilation, the second definiton of the
duplicate class would overwrite the first.

Further, that missing class is still missing - so the code
will not compile.

My advice to post an SSCCE is still (I feel) your best bet
at progressing this problem. If you did not read the document,
I *strongly* recommend you do so (there is more to it, than
first you might expect). I doubt anyone will wade
through the 300+ lines of uncompilable code and spot
the problem.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
E

Esmond Pitt

Robot said:
// client thread
class DoClient implements Runnable, svIF {
private static Socket s = null; //
private static ObjectInputStream in = null; //
private static ObjectOutputStream out = null; //
private static String ThisDN = null; //

None of those objects should be static. Make them all instance
variables. No wonder all hell breaks loose!
static ObjectInputStream in = null;
static ObjectOutputStream out = null;

static String ip = null;
static int port = 0;
static String name = null;

Ditto.

This will probably force some of your static methods to become
non-static as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top