当前位置: 代码迷 >> J2EE >> 求 Socket的有关问题!
  详细解决方案

求 Socket的有关问题!

热度:65   发布时间:2016-04-22 02:13:58.0
求高手指点 Socket的问题!!!!!!!!!!!!
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
*
*服务端
*/
public class ServiceSocket {
private static Socket socket;
private static ServerSocket server;

/**
* @param args
*/
public static void main(String[] args) {
try {
socket=new ServerSocket(10000).accept();
new Thread(new SocketListener(socket)).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * socket信息处理
 * @author Administrator
 *
 */
public class SocketListener implements Runnable{
private Socket client;
private BufferedReader in;
private PrintWriter out;
public SocketListener(Socket socket){
this.client=socket;
}
@Override
public void run() {
try {
while(true){
in=new BufferedReader(new InputStreamReader(client.getInputStream()));
out=new PrintWriter(client.getOutputStream());
out.print("++++++++++++++welcome+++++++++++++++++");
String line=in.readLine();
System.out.println(line);
while(!"byb".equals(line)){
System.out.println(client.getInetAddress()+":"+line);
out.println(client.getInetAddress()+"hello!!!!");
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 客户端
 * @author Administrator
 *
 */
public class SocketClient {

/**
* @param args
*/
public static void main(String[] args) {
  try {
Socket socket=new Socket("127.0.0.1",10000);
PrintWriter out=new PrintWriter(socket.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("sdjfkljsdlkfjskldjfklsdf");
System.out.println(in.readLine());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

启动以后什么都不显示 怎么回事 !高手帮帮忙!!!!

------解决方案--------------------
你处理消息那个方法好像有问题。
------解决方案--------------------
探讨
什么问题呢? 悲剧死了 也不报错 感觉好像是String line=in.readLine(); 读取客户端信息的地方阻塞了

------解决方案--------------------
我记得socket 有个阻塞的方法
socket.appset(); 【貌似字母打错了 等待监听、】
  相关解决方案