1.首先需要一个普通的对象类,由于需要序列化这个对象以便在网络上传输,所以实现java.io.Serializable接口就是必不可少的,请求实体:
/**
* @Title:RequestBean.java
* @Package:com.sk.bean
* @Description:请求参数
* @Author:shenkang
* @Date:2014年8月8日
* @Version:V1.0
*/
package com.sk.bean;
import java.io.Serializable;
/**
*
*/
public class RequestBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
//可以放任何类型的数据
private String name;
private String password;
/**
*
*/
public RequestBean() {
}
/**
* @param name
* @param password
*/
public RequestBean(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.响应实体
/**
* @Title:ResponseBean.java
* @Package:com.sk.bean
* @Description:响应参数
* @Author:shenkang
* @Date:2014年8月8日
* @Version:V1.0
*/
package com.sk.bean;
import java.io.Serializable;
/**
*
*/
public class ResponseBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String pwd;
private int age;
private String sex;
/**
*
*/
public ResponseBean() {
}
/**
* @param id
* @param name
* @param pwd
* @param age
* @param sex
*/
public ResponseBean(int id, String name, String pwd, int age, String sex) {
this.id = id;
this.name = name;
this.pwd = pwd;
this.age = age;
this.sex = sex;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
3.服务端:
/**
* @Title:SocketServer.java
* @Package:com.sk.socket
* @Description:服务端
* @Author:shenkang
* @Date:2014年8月8日
* @Version:V1.0
*/
package com.sk.socket;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import org.apache.log4j.Logger;
import com.sk.bean.RequestBean;
import com.sk.bean.ResponseBean;
/**
*
*/
public class SocketServer {
private final static Logger logger = Logger.getLogger(SocketServer.class
.getName());
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(6666);
while (true) {
logger.info("scoket start!");
Socket socket = server.accept();
invoke(socket);
}
}
private static void invoke(final Socket socket) throws IOException {
new Thread(new Runnable() {
public void run() {
ObjectInputStream is = null;
ObjectOutputStream os = null;
try {
is = new ObjectInputStream(new BufferedInputStream(
socket.getInputStream()));
os = new ObjectOutputStream(socket.getOutputStream());
Object obj = is.readObject();
RequestBean req = (RequestBean) obj;
System.out.println("user: " + req.getName() + "/"
+ req.getPassword());
//TOD 获取请求参数后可以查询数据信息
ResponseBean res = new ResponseBean(1, "sk", "123456", 22, "男");
os.writeObject(res);
os.flush();
} catch (IOException ex) {
logger.info("读取数据出错!");
} catch (ClassNotFoundException ex) {
logger.info("缺少类信息!");
} finally {
try {
is.close();
} catch (Exception ex) {
}
try {
os.close();
} catch (Exception ex) {
}
try {
socket.close();
} catch (Exception ex) {
}
}
}
}).start();
}
}
4.客户端:
/**
* @Title:SocketClient.java
* @Package:com.sk.client
* @Description:客户端
* @Author:shenkang
* @Date:2014年8月8日
* @Version:V1.0
*/
package com.sk.client;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import org.apache.log4j.Logger;
import com.sk.bean.RequestBean;
import com.sk.bean.ResponseBean;
/**
*
*/
public class SocketClient {
private final static Logger logger = Logger.getLogger(SocketClient.class
.getName());
public static void main(String[] args) throws Exception {
Socket socket = null;
ObjectOutputStream os = null;
ObjectInputStream is = null;
try {
socket = new Socket("127.0.0.1", 6666);
os = new ObjectOutputStream(socket.getOutputStream());
RequestBean req = new RequestBean("sk", "123456");
os.writeObject(req);
os.flush();
is = new ObjectInputStream(new BufferedInputStream(
socket.getInputStream()));
Object obj = is.readObject();
if (obj != null) {
ResponseBean res = (ResponseBean) obj;
System.out.println("age: " + res.getAge() + "/" + res.getSex());
}
} catch (IOException ex) {
logger.info("读取数据出错!");
} finally {
try {
is.close();
} catch (Exception ex) {
}
try {
os.close();
} catch (Exception ex) {
}
try {
socket.close();
} catch (Exception ex) {
}
}
}
}
5.先启动服务端,然后启动客户端:
服务端打印:user: sk/123456
客户端打印:age: 22/男