我在使用套接字的聊天应用程序时遇到问题。 我的服务器程序是用C#编写的,它能够支持多个客户端。服务器接收消息并将其发送回所有客户端。我尝试过,如果它适用于用C#编写的不同客户端(关于从服务器接收数据) 在我的Android客户端中,我成功连接到服务器并正确地向服务器发送消息。当我尝试使用套接字从服务器获取它们时,没有任何反应。 这是我连接到服务器的myMenu类。
public class myMenu extends Activity{
TextView textResponse;
EditText editTextAddress;
EditText editPort;//new guy :P
EditText editNick;//another new guy
public static String nick;
public static Socket socket = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button connect = (Button) findViewById(R.id.connectButton);
textResponse = (TextView)findViewById(R.id.response);
editTextAddress = (EditText)findViewById(R.id.ipaddress);
editPort = (EditText)findViewById(R.id.port);
editNick = (EditText)findViewById(R.id.nick);
//When clicked on connect button in main screen
connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MyClientTask myClientTask = new MyClientTask(editTextAddress.getText().toString(),
Integer.parseInt(editPort.getText().toString()));
myClientTask.execute();
nick = editNick.getText().toString();
if(socket!=null)
startActivity(new Intent("com.example.chatapp.CONNECTBUTTON"));
else
textResponse.setText("Connection failed");
}
});
}
public class MyClientTask extends AsyncTask<String,String,String> {
String dstAddress;
int dstPort;
String response = "";
MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
@Override
protected String doInBackground(String... params) {
Log.d("doInBackGround","socket Created");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
String line=null;
int bytesRead;
BufferedReader reader;
try {
socket = new Socket(dstAddress, dstPort);
InputStream inputStream = socket.getInputStream();
Log.d("doInBackGround","connected to server ");
InputStream dataComing = (InputStream)(myMenu.socket).getInputStream();
reader = new BufferedReader(new InputStreamReader(dataComing, "UTF-8"));
line=reader.readLine();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return line;
}
@Override
protected void onPostExecute(String line) {
// TODO Auto-generated method stub
super.onPostExecute(line);
if (line != null) {
TextView sentMessages;
sentMessages=(TextView)findViewById(R.id.sentMessages);
sentMessages.append((CharSequence) line);
}
}
}
}
在ConnectButton类中,我在点击发送按钮时将消息发送到服务器。
public class ConnectButton extends Activity{
public static EditText messageWritten;
public static TextView sentMessages;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.send_message);
messageWritten = (EditText) findViewById(R.id.editText1);
Button send = (Button) findViewById(R.id.sendButton);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*MessageDisplayer displayer= new MessageDisplayer();
displayer.execute();*/
//new MessageDisplayer().execute((String) null);
// TODO Auto-generated method stub
//use it in a method otherwise not working!messageWritten.getText().toString();
try {
PrintWriter out = new PrintWriter(new OutputStreamWriter(myMenu.socket.getOutputStream()));
final String message = messageWritten.getText().toString(); //get the text message on the text field
Log.d("onClick", "message written to string");
messageWritten.setText("");
out.write(myMenu.nick + ": " +message);
out.flush();
//out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
try {
PrintWriter out = new PrintWriter(new OutputStreamWriter(myMenu.socket.getOutputStream()));
out.write("exit");//when exit is written server disconnects from client
out.flush();
out.close();
myMenu.socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
毕竟,我希望程序从服务器恢复消息并在sentMessages textView上显示它们。我做错了什么我真的不明白。我的xml文件也在下面。提前致谢。 send_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.chatapp.MainActivity" >
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textView1"
android:layout_marginBottom="20dp"
android:text="@string/send" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/sendButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText1"
android:layout_centerHorizontal="true"
android:text="@string/enter_message"
android:textSize="15sp" />
<TextView
android:id="@+id/sentMessages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/editText1"
android:layout_marginTop="39dp"
android:text="@string/list_messages"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.chatapp.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:text="@string/enter_IP"
android:textSize="15sp" />
<EditText
android:id="@+id/ipaddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/textView1"
android:layout_marginTop="19dp"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="@string/hello_user"
android:textSize="25sp" />
<EditText
android:id="@+id/port"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/ipaddress"
android:layout_below="@+id/ipaddress"
android:layout_marginTop="22dp"
android:layout_toRightOf="@+id/textView3"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/connectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/response"
android:layout_alignLeft="@+id/textView1"
android:text="@string/connect" />
<TextView
android:id="@+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/connectButton"
android:layout_alignParentBottom="true"
android:layout_marginBottom="31dp"
android:layout_marginLeft="17dp"
android:text="@string/response" />
<EditText
android:id="@+id/nick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/connectButton"
android:layout_alignLeft="@+id/port"
android:layout_alignRight="@+id/port"
android:layout_marginBottom="34dp"
android:ems="10"
android:inputType="text" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/nick"
android:layout_alignRight="@+id/textView3"
android:text="@string/nick"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/port"
android:layout_alignLeft="@+id/ipaddress"
android:text="@string/port"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>