请教一个socket即时通信的问题,新人分不多,见谅
最近在写一个socket即时通讯的项目,一个服务端,一个客户端,写完后在测试的时候发现一个问题,就是客户端向服务端发送一条信息之后,连接就断开了,必须重新连接才能再发送消息,经测试应该是服务端写的有问题,但是我找不出来,请大神们帮我看看,谢谢!android socket --------------------编程问答-------------------- 有以下几个错误:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class SocketServer extends Service {
private int port = 3000;
private ServerSocket serverSocket;
private Socket socket;
private InputStream inputStream;
private BufferedReader bufferedReader;
public static String message;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("SocketServer onCreat----------");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Thread(new MyThread()).start();
return super.onStartCommand(intent, flags, startId);
}
class MyThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
// 接收数据
System.out.println("接收数据");
socket = serverSocket.accept();
// 创建套接字输入流
// if (socket.isConnected()) {
System.out.println("创建套接字输入流");
inputStream = socket.getInputStream();
// 根据inputstream创建bufferedReader对象
System.out.println("根据inputstream创建bufferedReader对象");
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream));
// 读取客户端传输的信息
System.out.println("读取客户端传输的信息");
message = bufferedReader.readLine();
if (null != message) {
Intent intent = new Intent();
intent.putExtra("msg", message);
intent.setAction("com.intent.action.msg");
sendBroadcast(intent);
System.out.println("message =" + message);
}
if (null != bufferedReader) {
bufferedReader.close();
inputStream.close();
socket.close();
}
// }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
1.
System.out.println("接收数据");
socket = serverSocket.accept();
这个可不是接收数据,accept是在接收连接。成功后才会返回这个socket。
2.
你竟然在while里面进行读取操作,而且还和accept在一起,大错特错,你只可能读取一次,第二次就阻塞了
3.主要原因是你没理解accept是在干什么
服务端至少需要2个线程来与客户端通信
thread1:不断accept新连接
thread2: 不断接收数据
剩下可以定义个按钮之类的发送数据
。。。。。。。。。。。。。。。。。
--------------------编程问答--------------------
确实是有种阻塞的感觉。。可是如果要不断接收数据的话,不写在while里写在哪里呢? --------------------编程问答-------------------- 你accept一次后就得到一个新socket,那个这个socket的输入输出流你就能获取,另起线程循环读取数据嘛 --------------------编程问答--------------------
您好,我写了两个线程还是没解决问题,麻烦您在帮我看一下,好人做到底,谢谢
public class SocketServer extends Service {
private int port = 3000;
private ServerSocket serverSocket;
private Socket socket;
private InputStream inputStream;
private BufferedReader bufferedReader;
public static String message;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("SocketServer onCreat----------");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// beginListener();
new Thread(new MyThread()).start();
return super.onStartCommand(intent, flags, startId);
}
class MyThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
socket = serverSocket.accept();
new Thread(new MyThread2()).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class MyThread2 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
try {
if (null != socket) {
bufferedReader = new BufferedReader(new InputStreamReader(
socket.getInputStream(), "UTF-8"));
// while (!socket.isClosed()) {
message = bufferedReader.readLine();
if (null != message) {
Intent intent = new Intent();
intent.putExtra("msg", message);
intent.setAction("com.intent.action.msg");
sendBroadcast(intent);
System.out.println("message =" + message);
}
// }
socket.close();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
补充:移动开发 , Android