android编程DataInputStream问题

qq_34675230 2017-12-15 06:57:20

//手机端
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
EditText show;
DataInputStream in;
DataOutputStream out;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show=(EditText) findViewById(R.id.show);
new Thread()
{
@Override
public void run()
{
try
{
show.append("聊天区域"+ "\n");
Socket so = new Socket("192.168.155.2" , 30000); //连接socket
in = new DataInputStream(so.getInputStream());
out=new DataOutputStream(so.getOutputStream());
while(true)
{
show.append("server:"+in.readUTF());//把接收的消息显示出来
show.append("\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}.start();
}
public void clickhander(View v) throws IOException {//发送按钮事件
EditText et=(EditText) findViewById(R.id.text1);
out.writeUTF(String.valueOf(et.getText()));
show.append("me:"+et.getText());
show.append("\n");
et.setText("");
}
}




//电脑端
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.*;
public class server extends JFrame implements ActionListener ,Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
InetAddress addr;
ServerSocket se;
Socket so;
DataInputStream in;
static DataOutputStream out;
JButton btn;
JTextField jtf;
JTextArea jta;
JScrollPane jsp;
server ()throws Exception
{
btn=new JButton("发送");
jtf=new JTextField(15);
btn.addActionListener(this);
this.setLayout(new BorderLayout());
jta=new JTextArea();
JPanel jp=new JPanel();
jp.add(btn);
jp.add(jtf);
jsp=new JScrollPane();
jsp.setViewportView(jta);
this.setTitle("server");
this.add(BorderLayout.SOUTH,jp);
this.add(jsp);
this.setSize(300, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addr=InetAddress.getLocalHost();
se=new ServerSocket(30000,10,addr);
System.out.println(se.getLocalSocketAddress());
System.out.println("服务器启动......");
so=se.accept();
System.out.println("有客户端连接......");
System.out.println(so.getRemoteSocketAddress());
in =new DataInputStream(so.getInputStream());
out=new DataOutputStream(so.getOutputStream());
}
public void run()
{

try
{
while(true)
{
jta.append("client:"+in.readUTF());
jta.append("\n");
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
try
{
out.writeUTF(jtf.getText());
jta.append("me:"+jtf.getText());
jta.append("\n");
jtf.setText(null);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Thread t=new Thread(new server());
t.start();
}

}

问题:手机端能给电脑端发送消息,一开始启动手机端程序时能通过DataInputStream获取消息,但是用电脑端发送数据时,手机端程序自动终止
...全文
282 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧