我想开发一个执行以下任务的服务器。1)接受client1的请求读取obj并写入另一个对象2)接受client2的请求读取obj并写入另一个对象3)client1输入client2的信息,服务器必须根据信息获取client2并在client2上写入一个对象
服务器 java
public class Server{
ServerSocket serverSocket ;
Socket socket;
@SuppressWarnings("rawtypes")
public static List<Map> clientList = new ArrayList<Map>();//creating list to store map objects of all clients
// Server socket instantiating
Server(int port) {
try{
serverSocket = new ServerSocket(port);
}catch(Exception e){
e.printStackTrace();
}
}
//server is waiting on listen mode for accepting clients
void serverConnect() {
while (true) {
try {
System.out.println("Server is Waiting for client to connect ");
socket = serverSocket.accept();
socket.setKeepAlive(true);
System.out.println("connected to: "+ socket.getRemoteSocketAddress());
new Thread(new ClientSession(socket)).start();//creating a new thread for every client
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
public static void main(String args[]) throws IOException {
Server server = new Server(5050);
try {
server.serverConnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
客户端会话.java
`public class ClientSession implements Runnable {
Socket clientsocket;
String emailId;
String msg;
String ipaddress;
String phoneNumber;
String sessionId;
String socketAddress;
String calleeInfo;
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/Server";
ObjectOutputStream oos;
@SuppressWarnings("rawtypes")
Map globalmap = new HashMap();//creating a hashmap object to store sockets,objectoutputstreamobjects of clients
ClientSession(Socket socket) {
// TODO Auto-generated constructor stub
globalmap.put("clientsockets", socket);
this.clientsocket = socket;
System.out.println("socket--"+socket);
try {
oos = new ObjectOutputStream(clientsocket.getOutputStream());
globalmap.put("oos", oos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void run() {
// TODO Auto-generated method stub
System.out.println("A new Thread started");
try {
System.out.println("IN SERVER READ METHOD");
ObjectInputStream ois = new ObjectInputStream(clientsocket.getInputStream());
Map<String, String> map = (Map<String, String>) ois.readObject();
System.out.println("map in servereread"+map);
System.out.println("IN SERVER DECODE METHOD");
msg = map.get("msg");
System.out.println(msg);
emailId = map.get("email");
phoneNumber = map.get("phoneno");
globalmap.put("phoneno", phoneNumber);
globalmap.put("email",emailId);
Server.clientList.add(globalmap);
if (msg.equals("REGISTER")) {
System.out.println("IN DECODE REGISTER");
ipaddress = map.get("ipaddr");
} else if (msg.equals("INVITE")) {
System.out.println("IN DECODE INVITE");
sessionId = map.get("sessionId");
calleeInfo = map.get("calleeInfo");
}
analyze(map);
} catch (Exception e) {
e.printStackTrace();
}
}
private void analyze(Map<String, String> map) {
// TODO Auto-generated method stub
try {
String SQL = null;
System.out.println(msg);
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
Connection conn = DriverManager.getConnection(DB_URL,"username","pwd");
Statement st = conn.createStatement();
SocketAddress socketAddr = clientsocket.getRemoteSocketAddress();
socketAddress = socketAddr.toString();
if (msg.equals("REGISTER")) {
SQL = "INSERT INTO Register (email,phoneNumber,ipaddress,socketaddress) VALUES ('"+ emailId+ "','"+ phoneNumber+ "','"+ ipaddress+ "','" + socketAddress + "')";
System.out.println("SQL statement..." + SQL);
serverWrite();
} else if (msg.equals("INVITE")) {
System.out.println("Connecting to database for invite...");
SQL = "INSERT INTO Invite (sessionid,calleremail,callerphoneNo,calleeInfo) VALUES ('"+ sessionId+ "','"+ emailId+ "','"+ phoneNumber+ "','" + calleeInfo + "')";
System.out.println("SQL statement--" + SQL);
System.out.println("CalleeInfo--" + calleeInfo);
// Retrieve the socket of the destination
String query = "SELECT * FROM Register WHERE email='"+ calleeInfo + "' OR phoneNumber = '" + calleeInfo+ "'";
ResultSet rs = st.executeQuery(query);
System.out.println("ResultSet--" + rs);
if (rs.next()) {
String socketaddr = rs.getString("socketaddress");
String email= rs.getString("email");
String phoneNo= rs.getString("phoneNumber");
String ipadd= rs.getString("ipaddress");
}
for(int i=0;i<Server.clientList.size();i++){
String emailid= (String) Server.clientList.get(i).get("email");
String phone = (String) Server.clientList.get(i).get("phoneno");
if(calleeInfo.equals(emailid) || calleeInfo.equals(phone)){
ObjectOutputStream out1= (ObjectOutputStream) Server.clientList.get(i).get("oos");
System.out.println("ObjectOutputStream----"+out1);
System.out.println("writing on"+Server.clientList.get(i).get("clientsockets"));
System.out.println("map before writing on to client2--"+map);
out1.reset();
out1.writeObject(map);
out1.flush();
break;
}
}
// retrieve socket info from register table based on calleeInfo
// as primary key
}
st.executeUpdate(SQL);
} catch (Exception e) {
e.printStackTrace();
}
}
private void serverWrite() {
// TODO Auto-generated method stub
try {
// Using ObjectOutputStream class to write object
HashMap<String, String> map = new HashMap<String, String>();// Creating an object for HashMap class
if (msg.equals("REGISTER")) {
System.out.println("ObjectOutputStream storing--"+oos);
map.put("msg", "REGISTERED"); // setting a message in HashMap object
System.out.println("sending registered");
oos.writeObject(map);// writing an object on socket
}
/* else if (msg.equals("INVITE")) {
map.put("msg", "CALLING");
System.out.println("sending calling");
}*/
} catch (Exception e) {
e.printStackTrace();
}
}
}
它要去client1套接字,但它没有写在client2上..它写在client1上..
提前致谢。