sockets - Writing client to server Java -


i need help. i'm making sample queue system. when try write data , store server, server returns exception. i'm newbie on sockets appreciate reply on question.

here's code student:

import java.lang.*; import java.io.*; import java.net.*; import java.util.*;  public class student {    public static void main(string args[]) {       scanner sc = new scanner (system.in);   try {      socket skt = new socket("localhost", 1234);      printwriter out = new printwriter(skt.getoutputstream(), true);       system.out.println("hello!");       system.out.print("please enter name: ");      string name = sc.next();      system.out.print("please enter id number: ");      string id = sc.next();      string student = name +" - "+ id;      out.print(student);        } catch(exception e) {           system.out.println("whoops! didn't work.");       }    } } 

and server:

import java.lang.*; import java.io.*; import java.net.*; import java.util.*;  public class server {    public static void main(string args[]) {       scanner sc = new scanner (system.in);       arraylist<string> collector = new arraylist<string>();    try {      serversocket srvr = new serversocket(1234);      socket skt = srvr.accept();      bufferedreader in = new bufferedreader(new inputstreamreader(skt.getinputstream()));       system.out.print(in.readline());      string x = in.readline();      collector.add(x);         }       catch(exception e) {          system.out.print("whoops! didn't work!\n");       }    } } 

you need close printwriter adding "out.close()" statement before finishes it's execution. since you'r enot doing that, client socket abruptly closed , exception on server side.

ideally close() should performed inside block make sure it's executed. also, pointed out in answer, should read once, store in variable, , whatever need value.

example:

public class server {     public static void main(string args[]) {         scanner sc = new scanner(system.in);         arraylist<string> collector = new arraylist<string>();          try {             serversocket srvr = new serversocket(1234);             socket skt = srvr.accept();             bufferedreader in = new bufferedreader(new inputstreamreader(skt.getinputstream()));              final string studentinfo = in.readline();             system.out.print(studentinfo);             collector.add(studentinfo);          } catch (exception e) {             e.printstacktrace();             system.out.println("whoops! didn't work!");         }     } } 

student:

public class student {     public static void main(string args[]) {         scanner sc = new scanner(system.in);         printwriter out = null;         try {             socket skt = new socket("localhost", 1234);             out = new printwriter(skt.getoutputstream(), true);              system.out.println("hello!");              system.out.print("please enter name: ");             string name = sc.next();             system.out.print("please enter id number: ");             string id = sc.next();             string student = name + " - " + id;             out.print(student);         } catch (exception e) {             system.out.println("whoops! didn't work.");         } {             if (out != null) {                 try {                     out.close();                 } catch (exception e) {                  }             }         }     } } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -