one simple problems in server echo ...
Hi !!
A have problem, when runing client and connect server (one client) its OK !, server write back text in console (simple echo server), when runing two client and want by server write back two client text, then three evetn its OK, (server back write text in client console) but then next event sending message its not OK, only one Client active (last client run) this first client only write text sending two Client.(server not take on text send first Client) . What its bad in source code?
This ist source code server:
import java.net.*;
import java.io.*;
import java.util.*;
class store
{
public String text = "";
public store next;
public store previous;
}
class createSocket{
public static ServerSocket socket;
public static Socket s;
public static int i=0,b=0;
public static String txt="";
public static store wartownik;
public static void createList()
{
wartownik = new store();
wartownik.next = wartownik;
wartownik.previous = wartownik;
wartownik.text = "end";
}
public void createSocket() throws IOException
{
try{
socket = new ServerSocket(1200);
createList();
while(true){
s = socket.accept();
Thread read = new readUTF();
read.start();
}
}catch(IOException){ }
socket.close();
s.close();
}
}
class sendUTF extends Thread
{
private DataOutputStream dos = readUTF.dos;
private String text = "end";
private store lastmsg,wartownik = createSocket.wartownik;
public void run()
{
while(true || false)
{
try{
dos.flush();
lastmsg = wartownik;
do{
lastmsg = lastmsg.next;
}while(lastmsg.next.text!=text);
do{
if(lastmsg.text!=text && lastmsg!=wartownik)
{
dos.writeUTF(lastmsg.text);
text = lastmsg.text;
lastmsg = lastmsg.previous;
}else { lastmsg=wartownik; }
}while(lastmsg!=wartownik);
sleep(2000);
}catch(IOException e){}
catch(InterruptedException e){}
}
}
}
class readUTF extends Thread
{
public static String inputText=" ";
public static DataOutputStream dos;//wysylanie danych
public static DataInputStream dis;
public static Socket socket = createSocket.s;
int a=0;
public static void add(String text)
{
store l = new store();
l.text = text;
l.next = createSocket.wartownik.next;
l.previous = createSocket.wartownik;
createSocket.wartownik.next.previous = l;
createSocket.wartownik.next = l;
}
public void run()
{
try{
dos = new DataOutputStream(createSocket.s.getOutputStream());
dis = new DataInputStream(createSocket.s.getInputStream());
Thread send = new Thread(new sendUTF());
send.start();
while(true || false)
{
add(dis.readUTF());
System.out.println(createSocket.wartownik.next.text);
}
}catch(Exception e){}
}
}
public class Server
{
public static void main(String args[]) throws IOException
{
createSocket cs = new createSocket();
cs.createSocket();
}
[3349 byte] By [
emilian85a] at [2007-10-3 9:57:41]

> class createSocket{
This is a terrible name for a class. A class isn't an action, it's a collection of state and operations. This class is actually the Server.
>
>public static ServerSocket socket;
This should not be static.
> public static Socket s;
This should not be static or even a class member. It should be local to the method that calls accept and it should be passed to the thread that is created to handle it as a constructor parameter. Otherwise all your client sockets are going to tread on each other's toes and anything at all could happen.
>public void createSocket() throws IOException
>{
>try{
> socket = new ServerSocket(1200);
> createList();
> while(true){
> s = socket.accept();
Socket s = socket.accept();
> Thread read = new readUTF();
Thread read = new readUTF(s);
> socket.close();
>s.close();
Delete the last line. (a) you shouldn't have an accepted socket in scope at this point, and (b) how do you know the client thread has finished?
> class sendUTF extends Thread
> {
>
>private DataOutputStream dos = readUTF.dos;
Don't do this. Get the output stream from the socket, and get the socket via the constructor. Again your client sockets and their streams are going to tread all over each other this way.
> try{
>
> dos.flush();
> lastmsg = wartownik;
> do {
> lastmsg = lastmsg.next;
>}while(lastmsg.next.text!=text);
>
>do{
>if(lastmsg.text!=text && lastmsg!=wartownik)
> {
>dos.writeUTF(lastmsg.text);
> text = lastmsg.text;
>lastmsg = lastmsg.previous;
> lastmsg=wartownik; }
>}while(lastmsg!=wartownik);
>sleep(2000);
> catch(IOException e){}
> catch(InterruptedException e){}
>}
>}
I can't make head or tail of that but you seem to be appending to a linked list and writing the last element. You don't seem to use the rest of the linked list: do you really need it at all? And if you do need it, there is a LinkedList class in the JDK, why wouldn't you use that?
> class readUTF extends Thread
Another poor class name. ReaderThread?
> {
>public static String inputText=" ";
> public static DataOutputStream dos;//wysylanie
This can't be static otherwise yet again your clients will tread on each other.
>public static DataInputStream dis;
Ditto
> public static Socket socket = createSocket.s;
Ditto
>
>public static void add(String text)
> {
>store l = new store();
>l.text = text;
>l.next = createSocket.wartownik.next;
>l.previous = createSocket.wartownik;
>createSocket.wartownik.next.previous = l;
>createSocket.wartownik.next = l;
>}
See above re LinkedList.
> public void run()
>
>try{
> dos = new
> DataOutputStream(createSocket.s.getOutputStream());
Again, don't do this, get 's' via the constructor of this class. You can't write a multi-client server this way.
> dis = new
> DataInputStream(createSocket.s.getInputStream());
Ditto.
And if this is just an echo server it's the most complicated one I've ever seen.
ejpa at 2007-7-15 5:15:55 >
