hi, bin ein zimlicher "new-be" was ServerSocket und Socket programmierung an geht und hab ein problem was ich nicht verstehe:
der Client:
Code:
import java.net.*;
import java.io.*;
public class Client extends Thread{
boolean running = true;
long id;
Socket s;
String out = null;
InputStream i;
OutputStream o;
/** Creates a new instance of Client */
public Client() throws Exception{
s = new Socket("localhost",7788);
run();
}
public void run() {
try{
System.out.println("Start Client");
i= s.getInputStream();
o= s.getOutputStream();
String out = "login;ClientName:password";
o.write(out.getBytes());
o.flush();
while(running){
String in = "";
while(true){
char c = (char)i.read();
if(c != '#'){
in += c;
}else{
break;
}
}
parser(in.split(";"));
}
}catch(Exception e){
e.printStackTrace();
}
}
public void parser(String[] cmd) throws Exception {
if(cmd[0].equals("login")){
o.write("id;get#".getBytes());
o.flush();
System.out.print("Hallo");
}if(cmd[0].equals("id")){
id = Long.parseLong(cmd[1]);
System.out.println(id);
}
}
public static void main(String[] args) throws Exception{
new Client();
}
}
der Server:
Code:
import java.net.*;
import java.io.*;
public class Server extends Thread{
ServerSocket s;
InputStream i;
OutputStream o;
long id = 0;
/** Creates a new instance of Server */
public Server() throws Exception {
s = new ServerSocket(7788);
start();
}
public void run(){
System.out.println("Start Server");
try{
while(true){
Socket s = this.s.accept();
i= s.getInputStream();
o= s.getOutputStream();
String in = "";
while(true){
char c = (char)i.read();
if(c != '#'){
in += c;
}else{
break;
}
}
parser(in.split(";"));
}
}catch(Exception e){
e.printStackTrace();
}
}
public void parser(String[] cmd) throws Exception {
if(cmd[0].equals("login")){
o.write("login;true#".getBytes());
}if(cmd[0].equals("id")){
o.write(("id;" + (id++) + "#").getBytes());
}
}
public static void main(String[] args) throws Exception{
new Server();
}
}
mein problem ist, dass wenn ich das prog starte, immer bis zur id abfrage beim client, also inder Zeile:
if(cmd[0].equals("id"))
weiter will er nicht. Warum??