socket通信,服务端怎么验证是否接收客户端发过来的数据.
一个南方人 2012-07-13 09:10:52 socket 服务端:
import java.io.IOException;
import java.net.ServerSocket;
public class listenserver {
/**
* @param args
*/
private ServerSocket ss;
private boolean listening=true;
public listenserver(){
init();
list();
}
private void init(){
try {
ss=new ServerSocket(9090,10);
System.out.println("开始在9090端口进行监听...");
} catch (IOException e) {
System.out.println("无法在端口9090下进行监听");
e.printStackTrace();
}
}
private void list(){
while(listening){
try {
new Thread(new dialogserve(ss.accept())).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new listenserver();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.SocketException;
public class dialogserve implements Runnable {
private Socket s;
private byte b[];
private String temp;
private String rev;
private InputStream is;
private int len;
public dialogserve(Socket ss) {
s = ss;
b = new byte[1024];
try {
is = s.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
rev = "";
}
public void run() {
try {
while (s.isConnected() == true) {
if ((len = is.read(b)) != -1) {
temp = new String(b, 0, len);
rev = temp;
System.out.println(rev);
temp = null;
Thread.sleep(1000);
}else{
return;
}
}
is.close();
s.close();
System.out.println("会话socket已断开!");
} catch (SocketException se) {
System.out.println("客户断已断开!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
已经测试可通过客户端直接发送数据到服务端.但服务端对客户端发送的数据没有任何验证,如何在服务端搞个登录名,如果是客户端发过来的数据,要通过验证才行.本人初学,各位大虾给个思路,或代码,求解.