|
好吧,问题自己查出来了- -悲剧了- - 不过还好
服务器端:
package sam.sucl;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Sample_5_1Server {
public static void main(String[]args){
ServerSocket ss=null;
Socket s=null;
DataInputStream din=null;
DataOutputStream dout=null;
try{
ss=new ServerSocket(50123);
System.out.println("已经监听到50123端口!");
}
catch(Exception e){
e.printStackTrace();
}
while(true){
try{
s=ss.accept();
System.out.println("S: Server...11");
dout=new DataOutputStream(s.getOutputStream());
System.out.println("S: Server...22");
din=new DataInputStream(s.getInputStream());
System.out.println("S: Server...33");
String msg=din.readUTF();
System.out.println("S: Server...44");
System.out.println("ip:"+s.getInetAddress());
System.out.println("S: Server...55");
System.out.println("msg:"+msg);
System.out.println("===================");
System.out.println("S: Server...66");
dout.writeUTF("Hello Clint!");
}
catch (IOException e){
e.printStackTrace();
}
finally{
try{
if (dout !=null){
dout.close();
}
if(din !=null){
din.close();
}
if(s!=null){
s.close();
}
}
catch (IOException e){
e.printStackTrace();
}
}
}
}
}
[/code]服务器端运行到33就无法进行:
客户端:
package sam.sucl;
import android.app.Activity;
import android.os.Bundle;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
public class Sample_5_1Activity extends Activity implements OnClickListener{
Button button;
EditText editText;
TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
editText=(EditText)findViewById(R.id.editText);
textView=(TextView)findViewById(R.id.textView);
button.setOnClickListener(this);
}
public void onClick(View v){
Socket s=null;
DataOutputStream dout=null;
DataInputStream din=null;
if(v==button)
try{
System.out.println("S: Connecting...11");
s=new Socket("169.254.112.19",50123);
System.out.println("S: Connecting...22");
dout=new DataOutputStream(s.getOutputStream());
System.out.println("S: Connecting...33");
din=new DataInputStream(s.getInputStream());
System.out.println("S: Connecting...44");
[/code]客户端运行到44就无法往下进行:
输出信息到这里,S: Connecting...44
main。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/editText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Hello Server">
</EditText>
<Button
android:text="发送信息到服务器"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:id="@+id/textView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="服务器发来的消息:"
/>
</LinearLayout>
[/code]
网络权限已写- - |
|