java代码client

来源:互联网 发布:贵州师范大学网络平台 编辑:程序博客网 时间:2024/04/27 13:57
package Client;import java.io.Serializable;import Server.ObjectTypeInterface;public class ClientDataUserLogin  implements Serializable,ObjectTypeInterface{private static final long serialVersionUID = 1L;public String userId = null;public String password = null;public String className = null;public ClientDataUserLogin() {this.className = "ClientDataUserLogin";this.userId = "ClientDataUserLogin";//测试专用}public String getUserId() {return this.userId;}public void setUserId(String userId) {this.userId = userId;}public void setPassword(String password) {this.password = password;}@Overridepublic String ObjectType() {// TODO Auto-generated method stubreturn this.className;}@Overridepublic String GetSenderUserId() {// TODO Auto-generated method stubreturn this.userId;}}
package ClientMain;public class ClientMain {public static void main(String[] args) throws InterruptedException {//ConnectServerData.setServerIP("127.0.0.1");ConnectServerData.setServerIP("172.16.28.38");ConnectServerData.setPort(8080);new SendMassage().start();new ReceiveMassage().start();while(true){Thread.sleep(2000);System.out.println(ReceiveMassageQueue.receiveMassageQueue.size());}}}

package ClientMain;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;import Server.ObjectTypeInterface;public class ConnectServer implements ObjectTypeInterface{public Socket socket   = null;public String serverIP = null;public int port ;public String className= null;public ConnectServer() throws UnknownHostException, IOException {this.className = "ConnectServer";serverIP = ConnectServerData.serverIP;port = ConnectServerData.port;SetConnectServerState();}public void SetConnectServerState() throws UnknownHostException, IOException {socket = new Socket(serverIP,port);}public Socket getSocket() {return socket;}@Overridepublic String ObjectType() {// TODO Auto-generated method stubreturn className;}@Overridepublic String GetSenderUserId() {// TODO Auto-generated method stubreturn null;}}

package ClientMain;// 该对象只用初始化一次,启动客户的时候,输入连接IP和Portpublic class ConnectServerData {public static String serverIP;public static int port;public static void setServerIP(String serverIP) {ConnectServerData.serverIP = serverIP;}public static void setPort(int port) {ConnectServerData.port = port;}}

package ClientMain;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.net.Socket;import java.net.UnknownHostException;import Server.ObjectTypeInterface;import Client.ClientDataUserLogin;public class ReceiveMassage extends Thread{ConnectServer connectServer = null;Socket socket = null;ObjectInputStream objectInputStream = null;ObjectOutputStream objectOutputStream = null;ObjectTypeInterface objectTypeInterface = null;public ReceiveMassage() {ReceiveMassageQueue.CreateReceiveMassageQueue(this);}public void run(){try {connectServer = new ConnectServer();socket = connectServer.getSocket();// 以下专用登陆功能*****************************************************objectOutputStream = new ObjectOutputStream(socket.getOutputStream());objectTypeInterface = (ObjectTypeInterface)new ClientDataUserLogin();objectOutputStream.writeObject(objectTypeInterface);//******************************************************************objectInputStream = new ObjectInputStream(socket.getInputStream());while(true){objectTypeInterface = (ObjectTypeInterface)objectInputStream.readObject();ReceiveMassageQueue.AddObject(objectTypeInterface);}} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

package ClientMain;import java.util.Vector;import Server.ObjectTypeInterface;public class ReceiveMassageQueue {public static Vector<ObjectTypeInterface> receiveMassageQueue = new Vector<ObjectTypeInterface>();public static Object receiveMassageQueueLock = null; // 这里应该接外部的锁public static void CreateReceiveMassageQueue(Object object) { //创建消息队列ReceiveMassageQueue.receiveMassageQueueLock = object;}   public static void AddObject(ObjectTypeInterface objectTypeInterface){receiveMassageQueue.add(objectTypeInterface);synchronized (receiveMassageQueueLock) {receiveMassageQueueLock.notify();}}public static void IsEmpty() throws InterruptedException{synchronized (receiveMassageQueueLock) {if(receiveMassageQueue.isEmpty()){receiveMassageQueueLock.wait();}}}public static ObjectTypeInterface GetObject(){ // 接口synchronized (receiveMassageQueueLock) {if(receiveMassageQueue.isEmpty()){return null;}else {ObjectTypeInterface objectTypeInterface = receiveMassageQueue.firstElement();//获取第一个元素receiveMassageQueue.removeElementAt(0);//删除第一个元素return objectTypeInterface;}}}}

package ClientMain;import java.io.IOException;import java.io.ObjectOutputStream;import java.net.Socket;import java.net.UnknownHostException;import Server.ObjectTypeInterface;public class SendMassage extends Thread{ConnectServer connectServer = null;Socket socket = null;ObjectOutputStream objectOutputStream = null;ObjectTypeInterface objectTypeInterface = null;Object sendMassageLock = null;public SendMassage() {SendMassageQueue.GetSendMassageQueueLocak(this);sendMassageLock = new Object();}public void run(){try {connectServer = new ConnectServer();socket = connectServer.getSocket();objectOutputStream = new ObjectOutputStream(socket.getOutputStream());while(true){synchronized (sendMassageLock) { //这里一定要同步SendMassageQueue.IsEmpty();objectTypeInterface = SendMassageQueue.GetObject();objectOutputStream.writeObject(objectTypeInterface);}}} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} }}

package ClientMain;import java.util.Vector;import Server.ObjectTypeInterface;public class SendMassageQueue {public static Vector<ObjectTypeInterface> sendMassageQueue = new Vector<ObjectTypeInterface>();private static Object sendMassageQueueLock = null;public static void GetSendMassageQueueLocak(Object object) {SendMassageQueue.sendMassageQueueLock = object; }public static void IsEmpty() throws InterruptedException{synchronized (sendMassageQueueLock) {if(sendMassageQueue.isEmpty()){sendMassageQueueLock.wait(); // 将线程唤醒}}}public static void AddObject(ObjectTypeInterface objectTypeInterface){ // 接口sendMassageQueue.add(objectTypeInterface);synchronized (sendMassageQueueLock) {sendMassageQueueLock.notify();}}public static ObjectTypeInterface GetObject(){synchronized (sendMassageQueueLock) {ObjectTypeInterface objectTypeInterface = sendMassageQueue.firstElement();//获取第一个元素sendMassageQueue.removeElementAt(0);return objectTypeInterface;}}}

package Server;import java.io.Serializable;public class ClientDataUserLoginState implements Serializable,ObjectTypeInterface{private static final long serialVersionUID = 2L;public String clientDataUserLoginState = null;public String UserId = null;public ClientDataUserLoginState(String clientDataUserLoginState) {// TODO Auto-generated constructor stubthis.clientDataUserLoginState = clientDataUserLoginState;}@Overridepublic String ObjectType() {// TODO Auto-generated method stubreturn this.clientDataUserLoginState;}@Overridepublic String GetSenderUserId() {// TODO Auto-generated method stubreturn this.UserId;}public String getUserId() {return UserId;}public void setUserId(String userId) {UserId = userId;}}

</pre><pre name="code" class="java">package Server;public interface ObjectTypeInterface {String ObjectType();String GetSenderUserId();}


0 0
原创粉丝点击