阅读更多
第一次把socket编程应用到实际项目中
场景就是现在很多公交车代理冲值,移动冲值那种方式
比如公交卡放到读卡器上 从电脑上操作,可以进行冲值操作
那么这里有个交互过程
第一:我们电脑里的应用程序去链接读卡器,读卡器返回给我们卡信息
第二:我们电脑里的应用程序进行卡冲值操作,把冲值命令发送给读卡器,读卡器对卡进行相应操作,然后返回操作结果给电脑的应用程序
操作完毕
这就是一个简单的socket编程实例
我把我前期写的例子贴出来,希望对大家有用:
定义一个serversocket
public class ThreadSever extends ServerSocket {
private static final int SERVER_PORT = 10000;
public ThreadSever()throws IOException {
super(SERVER_PORT);
try {
while (true) {
Socket socket = accept();
new CreateServerThread(socket);
}
}finally {
close();
}
}
class CreateServerThread extends Thread {
private Socket clientSocket;
private BufferedReader clientInputStream;
private BufferedWriter serverOutputSream;
public CreateServerThread(Socket socket) throws IOException {
clientSocket = socket;
clientInputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),"GB2312"));
serverOutputSream = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream(),"GB2312"));
debugMessage("start accept client socket...");
start();
}
public void run() {
try {
ClientXmlObject clientXmlObject = new ClientXmlObject();
debugMessage("start read client'data...");
clientXmlObject.readAll(clientInputStream);
debugMessage("start to read client'data in server:" + clientXmlObject.getXmlToString());
debugMessage("ent to read client'data in server.");
serverOutputSream.write(createMessage());
serverOutputSream.flush();
clientInputStream.close();
serverOutputSream.close();
clientSocket.close();
} catch (IOException e) {
}
}
private String createMessage(){
return "Hello!XuGuo,How are you?";
}
private void debugMessage(String message){
System.out.println(message);
}
}
public static void main(String[] args) throws IOException {
new ThreadSever();
}
}
客户端链接程序:
public class Client {
Socket socket;
BufferedReader serverInputStream;
BufferedWriter clientOutputSream;
public Client() {
try {
socket = new Socket("127.0.0.1", 10000);
debugMessage("made a client socket...");
clientOutputSream = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"GB2312"));
clientOutputSream.write("Hello!I'm client 001.");
clientOutputSream.flush();
debugMessage("writer a client'data...");
serverInputStream = new BufferedReader(new InputStreamReader(socket.getInputStream(),"GB2312"));
ServerXmlObject serverXmlObject = new ServerXmlObject();
debugMessage("read a server'data...");
serverXmlObject.readAll(serverInputStream);
System.out.println("start to read server'data in client:" + serverXmlObject.getXmlToString());
System.out.println("ent to read server'data in client.");
clientOutputSream.close();
serverInputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void debugMessage(String message){
System.out.println(message);
}
public static void main(String[] args) {
new Client();
}
}
两个数据操作的辅助类:
public class ClientXmlObject {
private StringBuffer sb = new StringBuffer();
private static final int STRING_BUFFER_SIZE = 5;
public void readAll(BufferedReader reader)throws IOException{
char[] charBuffer = new char[STRING_BUFFER_SIZE];
debugMessage("read server'data block...");
int hasMoreChar = reader.read(charBuffer);
String blockString = null;
if(hasMoreChar>0){
blockString = new String(charBuffer,0,hasMoreChar);
}else{
blockString = "";
}
debugMessage(blockString+"---");
debugMessage(hasMoreChar+"");
while(hasMoreChar!=-1){
sb.append(blockString);
if(sb.toString().endsWith("001.")){
break;
}
charBuffer = new char[STRING_BUFFER_SIZE];
debugMessage("read server'data block...");
hasMoreChar = reader.read(charBuffer);
if(hasMoreChar>0){
blockString = new String(charBuffer,0,hasMoreChar);
}else{
blockString = "";
}
debugMessage(blockString+"---");
debugMessage(hasMoreChar+"");
}
}
public String getXmlToString(){
return sb.toString();
}
private void debugMessage(String message){
System.out.println(message);
}
}
public class ServerXmlObject {
private StringBuffer sb = new StringBuffer();
private static final int STRING_BUFFER_SIZE = 5;
public void readAll(BufferedReader reader)throws IOException{
char[] charBuffer = new char[STRING_BUFFER_SIZE];
debugMessage("read server'data block...");
int hasMoreChar = reader.read(charBuffer);
String blockString = null;
if(hasMoreChar>0){
blockString = new String(charBuffer,0,hasMoreChar);
}else{
blockString = "";
}
debugMessage(blockString+"---");
debugMessage(hasMoreChar+"");
while(hasMoreChar!=-1){
sb.append(blockString);
if(blockString.endsWith("you?")){
break;
}
charBuffer = new char[STRING_BUFFER_SIZE];
debugMessage("read client'data block...");
hasMoreChar = reader.read(charBuffer);
if(hasMoreChar>0){
blockString = new String(charBuffer,0,hasMoreChar);
}else{
blockString = "";
}
debugMessage(blockString+"---");
debugMessage(hasMoreChar+"");
}
}
public String getXmlToString(){
return sb.toString();
}
private void debugMessage(String message){
System.out.println(message);
}
}