Java套接字服务器帮助

时间:2011-06-05 13:59:43

标签: java android sockets

您好,我对java编程非常新,我真的很难让套接字服务器和客户端按我的意愿工作。

我遇到的问题是......

服务器:

我正在查看客户端的输出,一旦客户端打印“TIME”,服务器将返回一条消息,例如“时间是......”。服务器执行此操作但不是马上就可以在第二次从客户端发送消息时发送它。

这是因为客户端一直没有连接吗?

我很确定这种方法是错误的,任何人都可以给我一些建议。

任何帮助都会很棒。

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class MyServer {

public static void main(String[] args){
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;

try {
serverSocket = new ServerSocket(8888);
System.out.println("Listening :8888");
} catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

 while(true){
 try {
socket = serverSocket.accept();
in = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));


dataOutputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
System.out.println("message: " + dataInputStream.readUTF());
dataOutputStream.writeUTF("Hello!");



try{
 String line = in.readLine();


 if (line.contains("TIME")){
  dataOutputStream.writeUTF("TIME IS.....");  // ITS HERE THE PROBLEM MAY BE ?

  {


    } catch (IOException e){
    System.out.println("Read failed");
    System.exit(1);
          }




 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  finally{
  if( socket!= null){
    try {
     socket.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}

if( dataInputStream!= null){
 try {
  dataInputStream.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}

if( dataOutputStream!= null){
 try {
  dataOutputStream.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
    }
   }
   }
   }
   }
   }

Android客户端

UPDATE ,我认为问题在于客户端仅在发送邮件时才连接。我如何在这里有一个读取循环,当我从客户端发送数据时不会影响。

package com.exercise.AndroidClient;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidClient extends Activity {

EditText textOut;
TextView textIn;

/** Called when the activity is first created. */
    @Override
  public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 textOut = (EditText)findViewById(R.id.textout);
 Button buttonSend = (Button)findViewById(R.id.send);
 textIn = (TextView)findViewById(R.id.textin);
 buttonSend.setOnClickListener(buttonSendOnClickListener);
 }

 Button.OnClickListener buttonSendOnClickListener
 = new Button.OnClickListener(){

 @Override
 public void onClick(View arg0) {
 // TODO Auto-generated method stub
 Socket socket = null;
 DataOutputStream dataOutputStream = null;
 DataInputStream dataInputStream = null;

 try {
 socket = new Socket("192.168.1.101", 8888);
 dataOutputStream = new DataOutputStream(socket.getOutputStream());
 dataInputStream = new DataInputStream(socket.getInputStream());
 dataOutputStream.writeUTF(textOut.getText().toString());
 textIn.setText(dataInputStream.readUTF());
 }  catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}};
}

2 个答案:

答案 0 :(得分:3)

dataOutputStream.writeUTF()行之后,写下dataOutputStream.flush();。这将通过发送所有数据。

答案 1 :(得分:0)

您似乎正在从流中读取输出消息。

    System.out.println("message: " + dataInputStream.readUTF());

因此,当您尝试运行

时,该命令已从流中删除
    String line = in.readLine();

在输出消息之前,您应该尝试将流中的命令读入变量,然后检查该字符串是否包含“TIME”。

为什么还要使用两种不同的方法从流中读取?您可以使用BufferedReader并丢弃DataInputStream。您也可以使用PrintWriter而不是DataOutputStream。

这样的事情:

    out = new PrintWriter( socket.getOutputStream(), true );

然后:

    out.println( "Time is..." );

希望这有帮助。