android socket 发送数据时 程序停止运行
代码是这样的:publicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState...
代码是这样的:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edittext = (EditText) findViewById(R.id.edittext);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendMsg();
}
});
}
// 发送信息
public void sendMsg() {
try {
// 创建socket对象,指定服务器端地址和端口号
socket = new Socket(IpAddress, Port);
// 获取 Client 端的输出流
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
// 填充信息
out.println(edittext.getText());
System.out.println("msg=" + edittext.getText());
// 关闭
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} 展开
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edittext = (EditText) findViewById(R.id.edittext);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendMsg();
}
});
}
// 发送信息
public void sendMsg() {
try {
// 创建socket对象,指定服务器端地址和端口号
socket = new Socket(IpAddress, Port);
// 获取 Client 端的输出流
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
// 填充信息
out.println(edittext.getText());
System.out.println("msg=" + edittext.getText());
// 关闭
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询