📄 m_client.java
字号:
// M_Client.java
//
// Set up a Client that will read information sent
// from a Server and display the information.
// Java core packets
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
// Java extension packets
import javax.swing.*;
public class M_Client extends JFrame
{
private JTextField enterField;
private JTextArea displayArea;
private DatagramPacket inPacket;
private DatagramPacket outPacket;
private DatagramSocket mClient;
private InetAddress sAddr;
private byte [] inBuffer;
private byte [] outBuffer;
private String message = "";
private InetAddress cAddr;
private int cPort;
// initialize chatServer and set up GUI
public M_Client(String s)
{
super( "Client" );
try
{
// set server to which this client connects
sAddr=InetAddress.getByName(s);
}
catch(UnknownHostException e)
{
System.err.println(e);
}
inBuffer=new byte[100];
Container container = getContentPane();
// create enterField and register listener
enterField = new JTextField();
enterField.setEnabled( false );
enterField.addActionListener(new ActionListener()
{
// send message to server
public void actionPerformed( ActionEvent e )
{
sendData( e.getActionCommand() );
}
} // end anonymous inner class
); // end call to addActionListener
container.add( enterField, BorderLayout.NORTH );
// create displayArea
displayArea = new JTextArea();
container.add( new JScrollPane( displayArea ),BorderLayout.CENTER );
setSize(500,300);
setVisible( true );
}
// connect to server and process messages from server
public void runClient()
{
// connect to server, get packets process packets
try
{
mClient=new DatagramSocket();
do
processPacket();
while(!message.equals( "SERVER>>> Byebye" )) ;
mClient.close();
}
// server closed connection
catch ( EOFException eofException )
{
System.out.println( "Server terminated connection" );
}
// process problems communicating with server
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}
private void processPacket() throws IOException
{
// enable enterField so client user can send messages
enterField.setEnabled( true );
// process messages sent from server
// read message and display it
try
{
inPacket= new DatagramPacket(inBuffer,inBuffer.length);
mClient.receive(inPacket);
cAddr=inPacket.getAddress();
cPort=inPacket.getPort();
displayArea.append( "\nPackage received from: ClientAddress="
+cAddr.toString()+" at Port "+cPort);
message =new String(inPacket.getData(),0,inPacket.getLength());
displayArea.append(message);
displayArea.setCaretPosition(displayArea.getText().length());
}
// catch problems reading from server
catch (IOException e)
{
displayArea.append( "\nIOExecption occurred with M_Client" );
}
} // end method process packet
// send message to server
private void sendData( String message )
{
// send object to server
try
{
outBuffer=("\n阎亚茹>>> " + message).getBytes();
outPacket=new DatagramPacket(outBuffer ,outBuffer.length,sAddr,8341);
mClient.send(outPacket);
enterField.setText("");
displayArea.append( "\n阎亚茹>>> " + message );
}
// process problems sending object
catch ( IOException e )
{
displayArea.append( "\nIOException occrred" );
System.out.println(e);
e.printStackTrace();
}
}
// execute application
public static void main( String args[] )
{
M_Client application;
String s=new String();
/*try
{ s=InetAddress.getLocalHost().getHostAddress();
}
catch(UnknownHostException e)
{
System.err.println(e);
} */
s="219.224.154.228";
if ( args.length == 0 )
application = new M_Client(s);
else
application = new M_Client(args[0]);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
application.runClient();
}
} // end class Client
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -