ServerSocket ss = null; DataInputStream is = null; DataOutputStream os = null; public SocketServer() { super(); try { init(); } catch (Exception e) { e.printStackTrace(); } } private void init() throws Exception { this.ss = new ServerSocket(PORT); //this.ss.setSoTimeout(0); }
public void run() { try {
Socket s = ss.accept(); while( s != null) { System.out.println("ServerSocket begin"); is = new DataInputStream(s.getInputStream()); os = new DataOutputStream(s.getOutputStream()); this.acceptData();
//s = null; s = ss.accept(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
import java.net.*;
import java.io.*;import com.yu.eis.ent.ReportData;
import com.yu.eis.log.L;public class SocketClient {
static private String STARTMARK = "DATA CLIENT START";
static private String ENDMARK = "DATA CLIENT END";
private int PORT =8000;
public static SocketClient dc = null;
Socket s = null; DataInputStream is = null; DataOutputStream os = null; static public SocketClient getInstance()
{
Object tmpObj = new Object();
synchronized(tmpObj)
{
if(SocketClient.dc == null)
{
synchronized(tmpObj)
{
SocketClient.dc = new SocketClient();
}
}
}
return SocketClient.dc;
}
private SocketClient() {
super();
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
} private void init() throws Exception {
L.debug("init");
s = new Socket("127.0.0.1", PORT); //获得socket
L.debug("new socket");
is = new DataInputStream(s.getInputStream());
os = new DataOutputStream(s.getOutputStream());
L.debug("is,os ok");
//s.setSoTimeout(3000);
L.debug("init end");
} public void uploadData(ReportData rd)
{
try {
L.debug("begin send");
os.writeUTF(STARTMARK);
os.writeUTF(rd.getGoodname());
os.writeUTF(rd.getMeasurementunit());
os.writeUTF(rd.getModule());
os.writeUTF(rd.getReporttime());
os.writeUTF(rd.getTypename());
//转换为字符串型
os.writeUTF(""+rd.getCurrentperiodstates());
os.writeUTF(""+rd.getGoodid());
os.writeUTF(""+rd.getInventory());
os.writeUTF(""+rd.getModuleprice());
os.writeUTF(""+rd.getPrevioussales());
os.writeUTF(""+rd.getReporttype());
os.writeUTF(""+rd.getRetailprice());
os.writeUTF(""+rd.getRetailsales());
os.writeUTF(""+rd.getSaleprice());
os.writeUTF(""+rd.getSales());
os.writeUTF(""+rd.getSalesvalue());
os.writeUTF(""+rd.getUploadtag());
os.writeUTF(""+rd.getUploadtype());
os.writeUTF(""+rd.getWholesaleprice());
os.writeUTF(""+rd.getWholesalessales());
os.writeUTF(this.ENDMARK);
L.debug("end send");
} catch (Exception e) {
e.printStackTrace();
}
}
public void close()
{
try
{
//os.close();
s.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
} public static void main(String[] args)
{
SocketClient socketClient1= new SocketClient();
for(int i=0;i<10;i++)
{
System.out.println("i="+i);
socketClient1.uploadData(new ReportData());
}
//socketClient1.close();
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}这是服务器端
package com.yu.test;
import java.io.*;
import java.net.*;import com.yu.eis.MidwareFactory;
import com.yu.eis.ent.ReportData;
import com.yu.eis.jo.Midreport;
import com.yu.eis.log.L;
import com.yu.eis.queue.Job;
import com.yu.eis.server.DataServer;public class SocketServer extends Thread
{
private int PORT =8000;
ServerSocket ss = null; DataInputStream is = null; DataOutputStream os = null; public SocketServer()
{
super();
try
{
init();
}
catch (Exception e)
{
e.printStackTrace();
}
} private void init() throws Exception
{
this.ss = new ServerSocket(PORT);
//this.ss.setSoTimeout(0);
}
public void run()
{
try {
Socket s = ss.accept();
while( s != null)
{
System.out.println("ServerSocket begin");
is = new DataInputStream(s.getInputStream());
os = new DataOutputStream(s.getOutputStream());
this.acceptData();
//s = null;
s = ss.accept();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void acceptData()
{
int i=0;
while (true)
{
try {
System.out.println("acceptDate");
String str = is.readUTF();//开始读数据
System.out.println(str);
Midreport mr = new Midreport();
L.debug("str");
if(str.equalsIgnoreCase(DataServer.STARTMARK))//开始的标志
{
System.out.println("start1");
str = is.readUTF();
System.out.println("start2");
mr.setGoodname(str);
L.debug(str);
str = is.readUTF();
mr.setMeasurementunit(str);
L.debug(str);
str = is.readUTF();
mr.setModule(str);
L.debug(str);
str = is.readUTF();
mr.setReporttime(str);
L.debug(str);
str = is.readUTF();
mr.setTypename(str);
L.debug(str);
//将字符串转换为其他类型
str = is.readUTF();
mr.setCurrentperiodstates(str);
L.debug(str);
str = is.readUTF();
mr.setGoodid(str);
L.debug(str);
str = is.readUTF();
mr.setInventory(Long.parseLong(str));
L.debug(str);
str = is.readUTF();
mr.setModuleprice(Double.parseDouble(str));
L.debug(str);
str = is.readUTF();
mr.setPrevioussales(str);
L.debug(str);
str = is.readUTF();
mr.setReporttype(Integer.parseInt(str));
L.debug(str);
str = is.readUTF();
System.out.println("start2");
mr.setRetailprice(Double.parseDouble(str));
L.debug(str);
str = is.readUTF();
mr.setRetailsales(Double.parseDouble(str));
L.debug(str);
str = is.readUTF();
mr.setSaleprice(Double.parseDouble(str));
L.debug(str);
str = is.readUTF();
mr.setSales(Long.parseLong(str));
L.debug(str);
str = is.readUTF();
mr.setSalesvalue(Double.parseDouble(str));
L.debug(str);
str = is.readUTF();
mr.setUploadtag(Integer.parseInt(str));
L.debug(str);
str = is.readUTF();
mr.setUploadtype(Integer.parseInt(str));
L.debug(str);
str = is.readUTF();
mr.setWholesaleprice(Double.parseDouble(str));
L.debug(str);
str = is.readUTF();
mr.setWholesalessales(Long.parseLong(str));
L.debug(str);
str = is.readUTF();
if(str.equalsIgnoreCase(DataServer.ENDMARK))//结束的标志
{
//L.debug("begin save data:");
//Job job = new Job(mr);
//MidwareFactory.getInstance().getQueue().push(job);
MidwareFactory.getInstance().getDataServer().saveData(mr);
L.debug("save data end.");
System.out.println("save data end");
}
}
System.out.println("Server accept: " +(++i)+" "+ str);
}
catch (SocketException e)
{
L.debug("There's one client quit: "+ e.getMessage());
System.out.println("Exception");
System.out.println(e.getMessage());
break;
}
catch (IOException e)
{
L.debug("There's some IOExeption: "+ e.getMessage());
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
SocketServer socketServer = new SocketServer();
socketServer.start();
}
}
你的代码真需要调整一下了,这样看代码好累呀这么长的数据输出却没看到flush动作,我也不知道socket和stream何时关闭的。Object tmpObj = new Object();
synchronized(tmpObj)
{
if(SocketClient.dc == null)
{
synchronized(tmpObj)
{
SocketClient.dc = new SocketClient();
}
}
}
这样的同步有何意义,为什么写两次?你把代码调整一下吧,少用static的东西。
漂亮的代码,即便有100个bug也容易查出
乱的代码,就算只有一个bug也要累死人