サーバ
public class Server {
class ServerHelper implements Runnable
{
InputStream is;
private InputStreamReader isr;
private BufferedReader br;
public ServerHelper(InputStream is) {
this.is = is;
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
}
private void display() throws IOException {
String s = "";
System.out.print("client says : ");
while ( ( s = br.readLine() ) != null ) {
System.out.print(s + " ");
}
}
@Override
public void run() {
try {
display( );
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
void start( ) throws Exception{
ServerSocket ss = new ServerSocket(5555);
while (true) {
System.out.println("waiting for conn..");
Socket accept = ss.accept();//code hangs over here and doesn't proceed ahead
if( accept == null )
System.out.println("got null...");
System.out.println("got the client req...");
ServerHelper sh = new ServerHelper(accept.getInputStream());
Thread t = new Thread(sh);
t.start();
}
}
public static void main(String[] args) {
try {
// TODO code application logic here
new Server().start();
} catch (Exception ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
クライアント
public class Client {
void start( ) throws Exception{
System.out.println("enter window size ");
Scanner sc = new Scanner(System.in);
int wsize = sc.nextInt();
Socket s = new Socket("127.0.0.1", 5555);
System.out.println("is connected .." + s.isConnected());
OutputStream outputStream = s.getOutputStream();
PrintWriter pw = new PrintWriter(outputStream);
String c = "y";
int j = 0;
do{
String se = "";
for (int i = 0; i < wsize; i++) {
j++;
se = se + String.valueOf(j);
}
pw.println(se);
pw.flush();
System.out.println("do u wanna send more....?(y|n)");
c = sc.next();
}while( c.equalsIgnoreCase("y") );
}
public static void main(String[] args) {
try {
// TODO code application logic here
new Client().start();
} catch (Exception ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Socket accept = ss.accept();
ここでコードがハングアップし、ioをブロックしていることはわかっていますが、クライアント側で、クライアントが接続されているかどうかを確認しましたが、接続されていると表示されています... accept() の問題は何ですか? 私はすべての TCP アプリケーションに対して同様の方法でコーディングしていますが、これは奇妙です..誰か助けてもらえますか
そのクラスを見てみたい人のために、ServerHelper コードも追加しました..