📄 simpleserversocket.java
字号:
else if (keyword.equals("MSG"))
{
String broadcast = st.nextToken();
//String userOption = st.nextToken();
StringBuffer message = new StringBuffer("MSG:" + broadcast);
message.append(st.nextToken("\0")); //????????????
if (broadcast.equals("BROAD"))
{
sendToClients(message); //ask all hotels regarding certain information
}
else
{
sendToClient(message, broadcast); //ask single hotel regarding certain information
}
}
else if (keyword.equals("QUIT"))
{
/////////////////////////////
ta.append("SYSTEM:" + this.name+ "/"
+ this.ipAddress +"log off the system.\n");
ps.println(new StringBuffer("QUIT:"+this.name));//Send client the log off message.
ps.flush();
socket.close(); //close the socket
array.remove(this);//delete the client from the client list array.
//refresh the client list array
list.removeAll();
for (int i = 0; i < array.size(); i++)
{
Client c = (Client) array.get(i);
list.add(c.name + "/" + c.ip
+" : connected to the system!", i);
}
refreshClientList();
break;
} //else if (keyword.equals("QUIT"))
}//while(true)
}//try
catch (IOException ex)
{
ta.append("SYSTEM:"+ex+"\n");
}
}
public void send(StringBuffer msg)
{
ps.println(msg);
ps.flush();
}
/////////////////////////////////////////////////////////////////////////////////////////
public String checkAvailability(int port,String roomType,String checkinDate,String checkoutDate)
{
Socket socket_hotel = null;
BufferedReader br_hotel = null; //for changing information between broker and the connected hotel
PrintStream ps_hotel = null; ////for changing information between broker and the connected hotel
String result = null;
try
{
socket_hotel = new Socket(InetAddress.getLocalHost(),port);
br_hotel = new BufferedReader(new InputStreamReader(socket_hotel.getInputStream()));
ps_hotel = new PrintStream(socket_hotel.getOutputStream());
StringBuffer msg = new StringBuffer("AVAILABILITY:");
msg.append(roomType + ":" + checkinDate + ":" + checkoutDate);
ps_hotel.println(msg);
ps_hotel.flush();
ta.append(msg + "\n");
ta.append(name+": Hotel Availability message sent"+"\n");
}
catch(IOException ex)
{
ex.printStackTrace();
ta.append("Error: "+ex+"\n");
}
try
{
String info = br_hotel.readLine();
result = info;
}
catch(Exception ex)
{
ex.printStackTrace();
}
try
{
socket_hotel.close();
ta.append("socket_hotel closed!\n");
}
catch(Exception ex)
{
ex.printStackTrace();
}
ta.append(result);
return result;
}
/////////////////////////////////////////////////////////////////////////////////////////
public String BookHotel(int port,String name, String phoneNumber, String creditNumber, String roomName, String checkinDate,String checkoutDate)
{
Socket socket_hotel = null;
BufferedReader br_hotel = null; //for changing information between broker and the connected hotel
PrintStream ps_hotel = null; ////for changing information between broker and the connected hotel
String result = null;
try
{
socket_hotel = new Socket(InetAddress.getLocalHost(),port);
br_hotel = new BufferedReader(new InputStreamReader(socket_hotel.getInputStream()));
ps_hotel = new PrintStream(socket_hotel.getOutputStream());
StringBuffer msg = new StringBuffer("BOOK:");
msg.append(name+ ":" + phoneNumber + ":" + creditNumber + ":" + roomName + ":" + checkinDate + ":" + checkoutDate );
/*ps_hotel.println(new StringBuffer("BOOK:"));
ps_hotel.println(new StringBuffer(name + ":"));
ps_hotel.println(new StringBuffer(phoneNumber + ":"));
ps_hotel.println(new StringBuffer(creditNumber + ":"));
ps_hotel.println(new StringBuffer(roomName + ":"));
ps_hotel.println(new StringBuffer(checkinDate + ":"));
ps_hotel.println(new StringBuffer(checkoutDate + ":"));*/
ps_hotel.println(msg);
ps_hotel.flush();
ta.append(name+": Hotel Booking message sent"+"\n");
}
catch(IOException ex)
{
ex.printStackTrace();
ta.append("Error: "+ex+"\n");
}
try
{
String info = br_hotel.readLine();
result = info;
}
catch(Exception ex)
{
ex.printStackTrace();
}
try
{
socket_hotel.close();
ta.append("socket_hotel closed!\n");
}
catch(Exception ex)
{
ex.printStackTrace();
}
ta.append(result);
return result;
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
public ArrayList<String> HotelConnector(int port, String keyword)
{
Socket socket_hotel;
String message;
BufferedReader br_hotel; //for changing information between broker and the connected hotel
PrintStream ps_hotel; //for changing information between broker and the connected hotel
String ip;
String name = "Hotel Booking Broker";
ArrayList<String> result = new ArrayList<String>();
try
{ socket_hotel = new Socket(InetAddress.getLocalHost(),port);
br_hotel = new BufferedReader(new InputStreamReader(socket_hotel.getInputStream()));
ps_hotel = new PrintStream(socket_hotel.getOutputStream());
/*if(socket_hotel != null)
{
StringBuffer msg = new StringBuffer("CONNECT:");//send broker the "CONNECT" message
ip = InetAddress.getLocalHost().toString();
msg.append(name+":"+ip);
ps_hotel.println(msg); //sent message to the server for authentication and authorization
ps_hotel.flush();
ta.append(msg + "\n");
//ta.setText("");
ta.append(name+": Hotel CONNECT message sent"+"\n"); //ta is the text area for display user option
}//if*/
if(keyword.equals("TYPE"))
{
ps_hotel.println(new StringBuffer("TYPE:"));
ps_hotel.flush();
ta.append(name+": Hotel TYPE message sent"+"\n");
}//if
if(keyword.equals("SINGLE") || keyword.equals("DOUBLE") || keyword.equals("KING") || keyword.equals("SUITE") )
{
ps_hotel.println(new StringBuffer(keyword + ":"));
ps_hotel.flush();
ta.append(keyword+"\n");
ta.append(name+": Get Empty room message sent"+"\n");
}//if
}//try
catch(IOException ex)
{
ex.printStackTrace();
ta.append("Error: "+ex+"\n");
return null;
}
while(true)
{
try
{
String info = br_hotel.readLine();
if(info == null)
{
ta.append("info reading finished\n");
break;
}
ta.append(info+"\n");
result.add(info);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}//while(true)
try
{
socket_hotel.close();
ta.append("socket_hotel closed!\n");
}
catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
}
///////////////////////////////////////////////////////////////
public synchronized void sendToClients(StringBuffer msg) //send messages to all of the hotels
{
for(int i=0;i<array.size();i++)
{
Client c = (Client)array.get(i);
c.send(msg);
}
}
public synchronized void sendToClient(StringBuffer msg,String s) //send message to single hotels
{
for(int i=0;i<array.size();i++)
{
Client c = (Client)array.get(i);
if(c.name.equals(s))
{
c.send(msg);
break;
}
}
}
// public synchronized void
/////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -