S
Srubys
hiya
The main problem is that server doesn’t receive any messages from
client( it receives a message from client only when I terminate the
client process ).
At first I thought that firewall was somehow preventing the two from
communicating, so I shut it down, but programs still couldn’t
communicate ( for the sake of readability I excluded try statements ):
SERVER SIDE:
public static void main(String[] args) {
Server.Init();
Server.handleClient();
}
class Server{
static int port = 2000;
static ServerSocket servs;
static Socket s;
static void Init(){
servs = new ServerSocket(port);
}
static void handleClient() throws IOException{
do{
Server s= new Server();
s.handleClient();
} while(true);
}
void handleClient() throws IOException{
s = servs.accept();
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bf = new BufferedReader(isr);
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bfw = new BufferedWriter(osw);
String messages;
messages = bf.readLine();
System.out.println ( “this line won’t get written to the
console”);
while ( ! messages.equals("Stop") ){
bfw.write ( “this is what you’ve send me” + messages );
bfw.flush();
System.out.println( messages );
messages = bf.readLine();
}
s.close();
}
}
CLIENT SIDE:
static Socket s;
static String message, response;
public static void main(String[] args) {
send(1600);
}
static void send( int port ) {
InetAddress ia = InetAddress.getLocalHost();
s = new Socket( is, port );
OutputStreamWriter ow = new OutputStreamWriter(
s.getOutputStream(),"UTF8");
BufferedWriter bw = new BufferedWriter(ow);
InputStreamReader ir = new InputStreamReader(
s.getInputStream(),
"UTF8");
BufferedReader br = new BufferedReader(ir);
InputStreamReader console = new InputStreamReader(
System.in );
BufferedReader readConsole = new BufferedReader(
readConsole );
while ( message != "Stop" ){
message = readConsole.readLine();
bw.write(message);
bw.flush();
response = br.readLine(); // here the programs waits
and waits
and…
System.out.println ( " This was sent by server:");
System.out.println ( response );
}
s.close();
thank you
The main problem is that server doesn’t receive any messages from
client( it receives a message from client only when I terminate the
client process ).
At first I thought that firewall was somehow preventing the two from
communicating, so I shut it down, but programs still couldn’t
communicate ( for the sake of readability I excluded try statements ):
SERVER SIDE:
public static void main(String[] args) {
Server.Init();
Server.handleClient();
}
class Server{
static int port = 2000;
static ServerSocket servs;
static Socket s;
static void Init(){
servs = new ServerSocket(port);
}
static void handleClient() throws IOException{
do{
Server s= new Server();
s.handleClient();
} while(true);
}
void handleClient() throws IOException{
s = servs.accept();
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bf = new BufferedReader(isr);
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bfw = new BufferedWriter(osw);
String messages;
messages = bf.readLine();
System.out.println ( “this line won’t get written to the
console”);
while ( ! messages.equals("Stop") ){
bfw.write ( “this is what you’ve send me” + messages );
bfw.flush();
System.out.println( messages );
messages = bf.readLine();
}
s.close();
}
}
CLIENT SIDE:
static Socket s;
static String message, response;
public static void main(String[] args) {
send(1600);
}
static void send( int port ) {
InetAddress ia = InetAddress.getLocalHost();
s = new Socket( is, port );
OutputStreamWriter ow = new OutputStreamWriter(
s.getOutputStream(),"UTF8");
BufferedWriter bw = new BufferedWriter(ow);
InputStreamReader ir = new InputStreamReader(
s.getInputStream(),
"UTF8");
BufferedReader br = new BufferedReader(ir);
InputStreamReader console = new InputStreamReader(
System.in );
BufferedReader readConsole = new BufferedReader(
readConsole );
while ( message != "Stop" ){
message = readConsole.readLine();
bw.write(message);
bw.flush();
response = br.readLine(); // here the programs waits
and waits
and…
System.out.println ( " This was sent by server:");
System.out.println ( response );
}
s.close();
thank you