私はJavaサーバーを書かれており、ここにコードされています:ServerSocket java-serverは入力を1回だけ読み込みますか?
try
{
ss = new ServerSocket(8080);
while (true)
{
socket = ss.accept();
System.out.println("Acess given");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//out = new PrintWriter(socket.getOutputStream(),true);
line = in.readLine();
System.out.println("you input is :" + in.readLine());
}
}
とiPhoneアプリケーションはクライアントであり、そのためのコードがあります:
- (void)viewDidLoad {
[super viewDidLoad];
socket = [[LXSocket alloc]init];
if ([socket connect:@"10.211.55.2" port:8080]) {
NSLog(@"socket has been created");
}
else {
NSLog(@"socket couldn't be created created");
}
@try {
}@catch (NSException * e) {
NSLog(@"Unable to send data");
}
[super viewDidLoad];
}
-(IBAction)sendData{
[socket sendString:@"A\n"];
}
私はここ2つの問題を抱えています:まず、サーバーが入力を1回だけ読み取っていることです。 2つ目は、データを出力しようとすると、メソッドを2回呼び出すまで出力されません(uibuttonを2回クリックするまで)。ここで何が起こっているのか分かりません。私は間違って何をしていますか?
while (server-socket is accepting new connections)
{
// The server-socket's job is to listen for connection requests
// It does this typically in a loop (until you issue server-shutdown)
// on accept the server-socket returns a Socket to the newly connected client
//
socket s = server-socket.accept-connection();
// various options here:
//
// typically fire off a dedicated thread to servie this client
// but also review NIO or (home-grown) connection-map/handler patterns
// the general pattern:
// create a dedicated thread per connection accepted.
// pass Socket (s) to the handler method (a Runnable) and start it off
// and that is it.
// Here we use the general pattern and create a dedicated
// handler thread and pass of the new connections' socket reference
//
Thread handler-thread = new Thread (handler-routine-as-runnable, s);
handler-thread.start();
}
あなたはあなたの[前の質問](http://stackoverflow.com/questions/6975410/serversocket-java-not-reading-inputstream)に受け入れた答えの最初の部分を実装していませんでした。それを行う。バッファリングはあなたをブロックしている可能性が非常に高いです。また、ある時点でストリームを閉じてください。 – Mat
ありがとうございます –