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();
}
}
}
启动以后什么都不显示 怎么回事 !高手帮帮忙!!!!
------解决方案--------------------
你处理消息那个方法好像有问题。
------解决方案--------------------
------解决方案--------------------
我记得socket 有个阻塞的方法
socket.appset(); 【貌似字母打错了 等待监听、】