客户端:
public static int sendMessage(Mesage msg){
if ("".equals(ip)){
Properties properties = new Properties();
try {
properties.load(****.class.getResourceAsStream("*****.properties"));
ip = properties.getProperty("SMS.IP");
port = Integer.parseInt(properties.getProperty("SMS.PORT"));
} catch (IOException e) {
e.printStackTrace();
}//加载Properties文件流
}
int i = 0;
try {
Socket smsSocket = new Socket(ip,port);
ObjectOutputStream smsObjStream = new ObjectOutputStream(smsSocket.getOutputStream());
smsObjStream.writeObject(smsBean);
smsObjStream.flush();
smsObjStream.close();
smsSocket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
i =0;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
i =0;
}
return i ;
}
服务端:
public void runServer(int port) {
// TODO Auto-generated method stub
ServerSocket messageServerSocket = null;
try {
messageServerSocket = new ServerSocket(port);
while(true){
try {
ObjectInputStream smsObjStream = null;
messagebean= null;
int i = 1;
try {
msgObjStream = new ObjectInputStream(this.socket.getInputStream());
messagebean= (messageBean) msgObjStream .readObject();
}catch (Exception e) {
e.printStackTrace();
continue;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try {
smsServerSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}