⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clients.java

📁 collection of java programs having gui applications
💻 JAVA
字号:
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Clients extends Panel
{
TextArea receivedText;
Socket sock;
private GridBagConstraints c;
private GridBagLayout gridBag;
private Frame frame;
private Label label;
JButton send;
JButton exit;
private int port=5001;
private TextArea sendText;
private String hostname;
private String username;
private DataOutputStream remoteOut;
ImageIcon i1,i2;
public static void main(String args[])
{
if(args.length != 2)
{
System.out.println("Format is : java Clients  ");
return;
}
Frame f1=new Frame("Welcome Protocol");
f1.resize(800,600);
f1.show();
JOptionPane.showMessageDialog(f1,"Welcome "+args[0]+".. Have a nice Session.","Welcome",0);
Frame f= new Frame("Connecting to Mr. "+args[0]);
Clients chat=new Clients(f,args[0],args[1]);
f.add("Center",chat);
f.setSize(800,600);
f.show();
chat.client();
}
public Clients(Frame f,String user,String host)
{
frame=f;
frame.addWindowListener(new WindowExitHandler());
username=user;
hostname=host;
label=new Label("Text to send :");
add(label);
sendText=new TextArea(15,30);
add(sendText);
label= new Label("Text received :");
add(label);

receivedText=new TextArea(15,30);
add(receivedText);
ImageIcon i1=new ImageIcon("click.gif");
ImageIcon i2=new ImageIcon("doorin2.gif");
send=new JButton("SEND",i1);
exit=new JButton("EXIT",i2);
add(send);
add(exit);
send.addActionListener(new TextActionHandler());
exit.addActionListener(new EXIT());
}

void client()
{
try
{
	if(hostname.equals("local"))
		hostname=null;
	InetAddress serverAddr= InetAddress.getByName(hostname);
	sock=new Socket(serverAddr.getHostName(),port);
	remoteOut=new DataOutputStream(sock.getOutputStream());
	System.out.println("Connected to server " + serverAddr.getHostName() + "on port " + sock.getPort());
	new ClientsReceive(this).start();
}
catch(IOException e)
	{
	System.out.println(e.getMessage() + " : Failed to connect to server.");
	}
}
protected void finalize() throws Throwable
{
try
{
	if(remoteOut != null)
		remoteOut.close();
	if(sock != null)
		sock.close();
}
catch(IOException x)
	{}
super.finalize();
}
class WindowExitHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window w=e.getWindow();
w.setVisible(false);
w.dispose();
System.exit(0);
}
}

class EXIT implements ActionListener
 {
   public void actionPerformed(ActionEvent e)
  {
   if((JOptionPane.showConfirmDialog(new Frame(),"Are You Sure to close the Session?"))==JOptionPane.YES_OPTION)
	{
	      JOptionPane.showMessageDialog(new Frame(),"Thank U. Visit Again.","Good Bye",0);
              System.exit(0);
	}

  }
 }
class TextActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
remoteOut.writeUTF(sendText.getText());
JOptionPane.showMessageDialog(new Frame(),"Your msg has been sent ","Confirmation",0);
sendText.setText("");
}
catch(IOException x)
{
System.out.println(x.getMessage() + " : connection to peer lost.");
}
}
}
}
class ClientsReceive extends Thread
{
private Clients chat;
ClientsReceive(Clients chat)
{
this.chat=chat;
}
public synchronized void run()
{
String s;
DataInputStream remoteIn=null;
try
{
	remoteIn= new DataInputStream(chat.sock.getInputStream());
while(true)
	{
	s=remoteIn.readUTF();
	chat.receivedText.setText(s);
	}
}
catch(IOException e)
{
System.out.println(e.getMessage() + " : connection to peer lost.");
}
finally
{
try
{
if(remoteIn !=null)
	remoteIn.close();
}
catch(IOException x)
{}
}
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -