android socket连接不上
安卓客户端向pc服务器发一个字符串,怎么都连接不上,求大神指导一下。服务器端代码:importjava.io.DataInputStream;publicclassser...
安卓客户端向pc服务器发一个字符串,怎么都连接不上,求大神指导一下。
服务器端代码:
import java.io.DataInputStream;
public class server {
public static void main(String[] args) {
try{
ServerSocket ss = new ServerSocket(10457);
InetAddress ipAddress = InetAddress.getLocalHost();
Socket socket = ss.accept();
DataInputStream dis = new DataInputStream(socket.getInputStream());
String msg = dis.readUTF();
System.out.println(msg);
}catch(Exception e){
e.printStackTrace();
};
}
}
android客户端代码:
package chen.button;
public class ButtonActivity extends Activity {
public Button button1;
String result = "123";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 =(Button) findViewById(R.id.button1);
button1.setOnClickListener(new Startsocketlistener());
}
class Startsocketlistener implements OnClickListener{
public void onClick(View v) {
new ServerThread().start();
button1.setText("123");
}
class ServerThread extends Thread{
public void run(){
Socket socket = null;
try{
InetAddress serverAddr = InetAddress.getByName("222.18.168.175");
socket = new Socket(serverAddr,10457);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(result);
dos.close();
socket.close();
}
}catch(IOException e)
{};
}
}
}
} 展开
服务器端代码:
import java.io.DataInputStream;
public class server {
public static void main(String[] args) {
try{
ServerSocket ss = new ServerSocket(10457);
InetAddress ipAddress = InetAddress.getLocalHost();
Socket socket = ss.accept();
DataInputStream dis = new DataInputStream(socket.getInputStream());
String msg = dis.readUTF();
System.out.println(msg);
}catch(Exception e){
e.printStackTrace();
};
}
}
android客户端代码:
package chen.button;
public class ButtonActivity extends Activity {
public Button button1;
String result = "123";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 =(Button) findViewById(R.id.button1);
button1.setOnClickListener(new Startsocketlistener());
}
class Startsocketlistener implements OnClickListener{
public void onClick(View v) {
new ServerThread().start();
button1.setText("123");
}
class ServerThread extends Thread{
public void run(){
Socket socket = null;
try{
InetAddress serverAddr = InetAddress.getByName("222.18.168.175");
socket = new Socket(serverAddr,10457);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(result);
dos.close();
socket.close();
}
}catch(IOException e)
{};
}
}
}
} 展开
2013-11-01
展开全部
看出错信息
看网络环境
看网络环境
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询