关于socket通信的android与pc端连接,为什么我定义的socket是为空的,连接
服务器端代码:
package mytable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MyServer implements ActionListener {
/**
* 服务器端
* */
private Window window;
private ServerSocket server;
private static Socket socket;
private JButton button;
private static JTextArea area;
private JTextField field;
// private BufferedWriter writer;
private OutputStream writer;
private static BufferedReader reader;
// 存放TextField中的字符串
private static String fromClient;
public MyServer() {
window = new Window("服务器:");
button = window.getButton();
area = window.getJTextArea();
field = window.getTextField();
button.addActionListener(this);
try {
//监听端口5000
server = new ServerSocket(5000);
System.out.println("服务器已创建,正在等待客户端连接。。。");
socket = server.accept();
System.out.println("客户端已连接");
//打开输入输出流
writer = socket.getOutputStream();
reader = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
new MyServer();
// 创建线程去读取客户端发来的数据
new Thread(new Runnable() {
@Override
public void run() {
try {
while (socket != null) {
fromClient = reader.readLine();
area.append("客户端:" + fromClient + '\n');
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
reader.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}).start();
}
/**
* 按钮监听方法,点击按钮时触发该方法。
* */
@Override
public void actionPerformed(ActionEvent e) {
String me = field.getText().toString() + '\n';
area.append("服务器:" + me);
field.setText("");
try {
writer.write(me.getBytes("utf-8"));
//刷新输出流
writer.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
try {
writer.close();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
}
客户端代码
package com.example.client;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.net.Socket;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
public EditText editText;
public Button button;
// 输出流
public BufferedReader in;
// 输入流
// private BufferedWriter out;
//输入流
// private InputStream in;
//输出流
public OutputStream out;
// 客户端socket
public Socket socket;
// 显示数据
public TextView textView;
// 用来存放要传递给客户端的数据
public static String data;
//handler发送处理消息
public Handler handler;
public String b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.send);
textView = (TextView) findViewById(R.id.textView);
//设置textView可以滚动
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if(msg.what == 0x1314)
{
textView.append("服务器:" + b + '\n');
}
return true;
}
});
new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket("127.0.0.1", 5000);
Log.e("--->", "已发出链接请求");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = socket.getOutputStream();
while (socket != null) {
b = in.readLine();
// textView.append("服务器:" + b + '\n');
handler.sendEmptyMessage(0x1314);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
data = editText.getText().toString();
textView.append("客户端说:" + data + '\n');
editText.setText("");
try {
if (socket != null) {
out.write((data + '\n').getBytes("GBK"));
out.flush();
}
else if(socket==null)
Log.e("--->", "服务器未连接,无法获得输出流");
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
|
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|