客户端:
模拟服务器端:
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class ClientSocketUtil {
protected static Socket server;
// private String url="";
// private int port=9766;
private boolean isConnected=true;
public ClientSocketUtil(int port){//链接自己,本机上测试的时候用
try {
server = new Socket(InetAddress.getLocalHost(), port);
} catch (Exception e) {
isConnected=false;
}
}
public ClientSocketUtil(String url,int port){
try {
server = new Socket(url,port);
} catch (Exception e) {
isConnected=false;
}
}
// 向服务端程序发送数据
public void send(String data){
try {
OutputStreamWriter osw = new OutputStreamWriter(server.getOutputStream());
BufferedWriter bw = new BufferedWriter(osw);
bw.write(data+"\r\n");
bw.flush();
// DataOutputStream dos=new DataOutputStream(server.getOutputStream());
// dos.write(data.getBytes());
// dos.flush();
} catch (IOException e) {
}
}
/**
* 从服务端程序接收数据,返回一个BufferedReader
* @return
*/
public BufferedReader recieve(){
InputStreamReader isr=null;
BufferedReader br=null;
try {
isr = new InputStreamReader(server.getInputStream(),"GBK");
br = new BufferedReader(isr);
} catch (IOException e) {
}
return br;
}
public void close(){
try {
if(server!=null||server.isConnected()){
server.close();
}
} catch (IOException e) {
}
}
public boolean isConnected() {
return isConnected;
}
public void setConnected(boolean isConnected) {
this.isConnected = isConnected;
}
public static void main(String[] args) {
ClientSocketUtil util=new ClientSocketUtil(9766);
util.send("aaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccc");
BufferedReader br=util.recieve();
String s = "";
try {
while((s = br.readLine()) != null)
System.out.println(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
util.close();
}
}
模拟服务器端:
import java.io.*;
import java.net.*;
import java.util.List;
import java.util.Map;
import com.worthtech.app.util.Constants;
public class ServerSocketConnection extends Thread{
private Socket client;
ServerSocketConnection(Socket client){
this.client=client;
}
public void run(){
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream());
// while (true) {
String data = in.readLine();
System.out.println("『服务器收到』"+data);
///
String[] s=new String[11];
int pos=0;
s[0]=data.substring(pos,6);
pos+=6;
s[1]=data.substring(pos, 12);
pos+=6;
s[2]=data.substring(pos, 24);
pos+=12;
s[3]=data.substring(pos, 39);
pos+=15;
s[4]=data.substring(pos, 51);
pos+=12;
s[5]=data.substring(pos, 59);
pos+=8;
s[6]=data.substring(pos, 71);
pos+=12;
s[7]=data.substring(pos, 87);
pos+=16;
s[8]=data.substring(pos, 105);
pos+=18;
s[9]=data.substring(pos, 112);
pos+=7;
s[10]=data.substring(pos, 117);
pos+=5;
for(int i=0;i<s.length;i++){
System.out.println("s"+i+"="+s[i]);
}
// DBConnection db = new DBConnection();
// String sql="select * from user where phone='"+phone+"'";
// List list=null;
// try {
// db.getCurrentConnection();
// list = db.executeQueryList(sql);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
System.out.println("list.size="+list.size());
// db.closeCurrentConnection();
// if(list==null||list.size()==0){//没有记录
// out.println("没有查到该数据。");//向客户端发送
// }else{
// String password=(String) ((Map)list.get(0)).get("password");
// out.println("您的密码为:"+password+",请妥善保存!");//向客户端发送
// }
out.println("服务器成功收到数据!");//向客户端发送
// out.println("服务器收到了,这是返回信息。");//向客户端发送
///
out.flush();
System.out.println("---------send success-----------");
// if (str.equals("end"))
// break;
// }
client.close();
} catch (IOException ex) {
} finally {
}
}
public static void main(String[] args) throws IOException{
//这个程序的主要目的在于服务器不断接收客户机所写入的信息只到。客户机发送"End"字符串就退出程序。
//并且服务器也会做出"Receive"为回应。告知客户机已接收到消息。多线程
ServerSocket server=new ServerSocket(Constants.port);
while(true){
ServerSocketConnection ssc=new ServerSocketConnection(server.accept());
ssc.start();
}
}
}