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

📄 socketclient.java

📁 用Java实现的网络画图程序
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class SocketClient extends BaseClient{

	SketchView view;
	
	public  SocketClient(SketchView view,String user,String password,String host ) throws Exception{
		SocketClientInit(view,user,password,host);
	}

	public  void SocketClientInit(SketchView view,String user,String password,String host ) throws Exception{
		userid=user;
		try{	
			server =new SocketStub(user,password,host);
			this.view=view;
			displayHistory() ;
		}catch(Exception e){
		JOptionPane.showMessageDialog(this,"登录错误,请检查服务器地址、用户名和密码是否正确","登录错误",JOptionPane.ERROR_MESSAGE);
			PerformedLogonFailedDialog dlg=new PerformedLogonFailedDialog();
			if(server!=null){
				
				server=null;
			}
			
			String user1=dlg.getUser();String password1=dlg.getPassword();String address1=dlg.getAddress();
			SocketClientInit( view, user1, password1,address1 );
		
		}
	}


	class SocketStub extends Thread implements ChatServer{
		protected  ServerSocket serverSocket;

		protected Socket sendSocket;
		protected Socket receiveSocket;

		protected DataOutputStream outOutput;
		protected DataInputStream outInput;
		protected ObjectOutputStream outOutputObject;
		protected ObjectInputStream inInput;
		
		public   SocketStub(String userid,String password,String host) throws IOException,LogonfailedException{
			
			setPriority(Thread.MAX_PRIORITY);
			start();
			performLogon(userid,password,host);
		}

		public void performLogon(String user,String password,String host) throws IOException,LogonfailedException{
			InputStream is;
			OutputStream os;
			
			InetAddress addr=InetAddress.getByName(host);
			sendSocket=new Socket(addr,SocketServer.PORT_NUMBER);
			is=sendSocket.getInputStream();
			outInput=new DataInputStream(is);
			os=sendSocket.getOutputStream();
			outOutputObject=new ObjectOutputStream(os);
			outOutput=new DataOutputStream(os);
			outOutput.writeUTF(user);
			outOutput.writeUTF(password);
			outOutput.writeInt(serverSocket.getLocalPort());
			int staus=outInput.readInt();
			if(staus==SocketServer.LOGON_FAILED){
				throw new LogonfailedException("Invalid userid/password");
				
			}
		}		
			

		public void run(){
			ChatMessage message;
			Object object;
			try{
				initialize();
				while(true){
					object=inInput.readObject();
					if(object instanceof ChatMessage)
						displayMessage((ChatMessage)object);
					else
						drawElement((Element)object);
				}
			}catch(Exception e){}
		}


		protected void initialize() throws IOException{
			InetAddress sendAddr;
			InetAddress recvAddr;
			InputStream is;
			serverSocket=new ServerSocket(0); 
			Socket s=serverSocket.accept();
			sendAddr=sendSocket.getInetAddress();
			recvAddr=s.getInetAddress();
			if(sendAddr.equals(recvAddr)){
				receiveSocket=s;
				is=receiveSocket.getInputStream();
				inInput=new ObjectInputStream(is);
			}
		}

		public void broadcastMessage(String message)throws IOException{
			outOutput.writeInt(SocketServer.INVOKE_BROADCAST);
			outOutput.writeUTF(message);
		}


		public void broadcastElement ( Element element ) throws IOException{
			
			outOutput.writeInt(SocketServer.INVOKE_ELEMENT);
			outOutputObject.writeObject(element);
			
		}

		public void drawElement(Element element){
			
			view.drawElement(element);
		}

		public ChatMessage[] getHistory() throws IOException {
			ChatMessage[] messages=null;
			
			int count;
			long timeValue;
			java.util.Date msgDate;
			String msgText;
			
			outOutput.writeInt(SocketServer.INVOKE_GET_HISTORY);
			count=outInput.readInt();
			messages=new ChatMessage[count];
			for(int i=0;i<count;i++){
				timeValue =outInput.readLong();
				msgText=outInput.readUTF();
				msgDate=new java.util.Date(timeValue);
				messages[i]=new ChatMessage(msgDate,msgText);
			}
			return messages;
		}
	
	}



	public void broadcastElement ( Element element ) {
			try{
			server.broadcastElement(element);
			}catch(Exception e){}
			
	}


	class PerformedLogonFailedDialog extends JDialog implements ActionListener{
		private JTextField addressField;
		private JTextField userField;
		private JPasswordField  passwordField;	
		private JButton confirmButton;

		private String user="梁志伟";
		private String password="djinhua**";
		private String address="localhost";
		
		public PerformedLogonFailedDialog(){
			super((JFrame)null,"登陆",true);
			buildLayout();
		}

		private void buildLayout(){
			JPanel panel=new JPanel();
			panel.setLayout(new GridLayout(4,1));
			JPanel addressPanel=new JPanel();
			addressPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
			addressPanel.add(new JLabel("服务器地址:"));
			
			addressPanel.add(addressField=new JTextField(20  ));
			addressField.setText("localhost");
			addressField.addActionListener(this);
			
			JPanel userPanel=new JPanel();
			userPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
			userPanel.add(new JLabel("用户名:        "));
			userPanel.add(userField=new JTextField( 20 ));
			userField.setText("梁志伟");
			userField.addActionListener(this);
			
			JPanel passwordPanel=new JPanel();
			passwordPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
			passwordPanel.add(new JLabel("密码:           "));
			passwordPanel.add(passwordField=new JPasswordField ( 20 ));
			passwordField.setText( "djinhua**");
			passwordField.addActionListener(this);

			JPanel controlPane=new JPanel();
			
			confirmButton=new JButton("确定");
			confirmButton.addActionListener(this);
			controlPane.add(confirmButton);
			
			JButton cancelButton=new JButton("取消");
			cancelButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					System.exit(0);
				}
			});
			controlPane.add(cancelButton);
			JPanel control=new JPanel();
			control.setLayout(new BorderLayout());
			control.add(controlPane,BorderLayout.CENTER);
		
			panel.add(addressPanel);
			panel.add(userPanel);
			panel.add(passwordPanel);
			panel.add(control);
			getContentPane().add(panel,BorderLayout.CENTER);
			

			Toolkit toolKit=getToolkit();
			Dimension size=toolKit.getScreenSize();
			setBounds((size.width-350)/2,(size.height-250)/2,350,250 );

			
			setVisible(true);
		}

		public void actionPerformed(ActionEvent e){
			 user=userField.getText();
			 password=passwordField.getText();
			 address=addressField.getText();
			 setVisible(false);
			
			
		}

		
		public String getUser(){
			return user;
		}

		public String getPassword(){
			return password;
		}

		public String getAddress(){
			return address;
		}



	}
			
		

	public static void main(String args[]) throws Exception{
		String userid=(args.length>0?args[0]:"zhiweiliang");
		String password=(args.length>1?args[1]:"zhiweiliang");
		String host=(args.length>2?args[2]:"localhost");

		SocketClient sc=new SocketClient(null,userid,password,"zhiweiliang");
		JDialog f=new JDialog((JFrame)null,"Chat Socket Client ("+userid+")",false);
		f.getContentPane().add(sc,BorderLayout.CENTER);
		f.setSize(400,300);
		f.setVisible(true);
	}

}
		

			

⌨️ 快捷键说明

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