2019.10.07

1.窗口聊天

代码:
服务器:
package MyFrame;

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyServer implements ActionListener {
	ServerSocket ss;
	Socket s;
	int port;
	String str;
	BufferedReader br;
	PrintStream p;
	JFrame f;
	JLabel l1,l2,l3;
	JButton b1,b2,b3;
	JTextArea ta;
	JTextField t1,t2,t3;
	public MyServer() {
		f=new JFrame("服务器端");
		l1=new JLabel("ip地址:");
		l2=new JLabel("端口号:");
		l3=new JLabel("服务器对话框:");
		b1=new JButton("确定");
		b2=new JButton("关闭");
		b3=new JButton("发送");
		t1=new JTextField(20);
		t2=new JTextField(20);
		t3=new JTextField(20);
		ta=new JTextArea();
		ta.setLineWrap(true);
		ta.setEditable(false);
		f.setSize(500, 600);
		f.setVisible(true);
		f.setLayout(null);
		f.add(l1);
		f.add(t1);
		f.add(l2);
		f.add(t2);
		f.add(l3);
		f.add(b1);
		f.add(b2);
		f.add(b3);
		f.add(ta);
		f.add(t3);
		l1.setBounds(50, 15, 50, 25);
		t1.setBounds(100, 15, 120, 25);
		l2.setBounds(250, 15, 50, 25);
		t2.setBounds(300, 15, 120, 25);
		b1.setBounds(160, 50, 60, 30);
		b2.setBounds(250, 50, 60, 30);
		ta.setBounds(0, 90, 500, 420);
		l3.setBounds(10, 520, 100, 30);
		t3.setBounds(100, 520, 300, 30);
		b3.setBounds(410, 520, 60, 30);
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
f.setBounds(100, 100, 500, 600);
	}
	public static void main(String[] args) throws IOException {
		new MyServer();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Thread t1=new Thread(new Runnable() {
			public void run() {
				try {
					ss=new ServerSocket(port);
					s=ss.accept();
					ta.append("客户端连接成功!\n");
					br=new BufferedReader(new InputStreamReader(s.getInputStream()));
					p=new PrintStream(s.getOutputStream());
					p.println("已成功连接服务器!");
					while(true) {
							str=br.readLine();
							ta.append("客户端:"+str+"\n");
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}});
		String str1=e.getActionCommand();
		if(str1.equals("确定")) {
				String str2=t2.getText();
				port=Integer.parseInt(str2);
				ta.append("服务器启动完成!等待连接中......\n");
				t1.start();
		}
		else if(str1.equals("关闭")) {
			System.exit(0);
		}
		else if(str1.equals("发送")){
			String str3=t3.getText();
			ta.append(str3+"\n");
			p.println(str3);
			t3.setText("");
		}
	}
}
客户端:
package MyFrame;

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyClient implements ActionListener {
	Socket s;
	int port;
	String str,ip1;
	BufferedReader br;
	PrintStream p;
	JFrame f;
	JLabel l1,l2,l3;
	JButton b1,b2,b3;
	JTextArea ta;
	JTextField t1,t2,t3;
	public MyClient() {
		f=new JFrame("客户端");
		l1=new JLabel("ip地址:");
		l2=new JLabel("端口号:");
		l3=new JLabel("客户端对话框:");
		b1=new JButton("确定");
		b2=new JButton("关闭");
		b3=new JButton("发送");
		t1=new JTextField(20);
		t2=new JTextField(20);
		t3=new JTextField(20);
		ta=new JTextArea();
		ta.setLineWrap(true);
		ta.setEditable(false);
		f.setSize(500, 600);
		f.setVisible(true);
		f.setLayout(null);
		f.add(l1);
		f.add(t1);
		f.add(l2);
		f.add(t2);
		f.add(l3);
		f.add(b1);
		f.add(b2);
		f.add(b3);
		f.add(ta);
		f.add(t3);
		l1.setBounds(50, 15, 50, 25);
		t1.setBounds(100, 15, 120, 25);
		l2.setBounds(250, 15, 50, 25);
		t2.setBounds(300, 15, 120, 25);
		b1.setBounds(160, 50, 60, 30);
		b2.setBounds(250, 50, 60, 30);
		ta.setBounds(0, 90, 500, 420);
		l3.setBounds(10, 520, 100, 30);
		t3.setBounds(100, 520, 300, 30);
		b3.setBounds(410, 520, 60, 30);
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
f.setBounds(700, 100, 500, 600);
	}
	public static void main(String[] args) throws IOException {
			new MyClient();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Thread t=new Thread(new Runnable() {
			public void run() {
				try { 
					s=new Socket(ip1,port);
					br=new BufferedReader(new InputStreamReader(s.getInputStream()));
					p=new PrintStream(s.getOutputStream());
					while(true){
							str=br.readLine();
							ta.append("服务器:"+str+"\n");
						}
				}
				catch (UnknownHostException e1) {
					ta.append("IP地址或端口号输入错误!");
					e1.printStackTrace();
				} 
				catch (ConnectException e2) {
					ta.append("找不到对应服务器!端口号输入错误!");
					e2.printStackTrace();
				}
				catch(SocketException e3) {
					ta.append("连接错误!");
					e3.printStackTrace();
				}
				catch (IOException e) {
						e.printStackTrace();
					}	 
			}
		});
		
		String str1=e.getActionCommand();
		if(str1.equals("确定")) {
			ip1=t1.getText();
			String str2=t2.getText();
			port=Integer.parseInt(str2);
			t.start();
			ta.append("客户端启动成功!正在连接中......\n");
		}
		else if(str1.equals("关闭")) {
			System.exit(0);
		}
		else if(str1.equals("发送")) {
			String str5=t3.getText();
			ta.append(str5+"\n");
			p.println(str5);
			t3.setText("");
		}
	}
}

运行截图:

2.文件传送

代码:
服务器:
package MyFileTCP;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.*;
import java.net.*;
public class MyFileServer implements ActionListener{
	String path;
	ServerSocket ss;
	Socket s;
	JFrame f;
	JPanel p;
	JTextArea ta;
	JLabel l;
	JButton b1,b2;
	JTextField t;
	public MyFileServer() {
		f=new JFrame("服务器端");
		p=new JPanel();
		ta=new JTextArea();
		l=new JLabel("文件路径:");
		b1=new JButton("发送");
		b1.addActionListener(this);
		b2=new JButton("关闭");
		b2.addActionListener(this);
		t=new JTextField(14);
		f.setVisible(true);
		f.setSize(360, 260);
		f.setLayout(new BorderLayout());
		f.add(ta,BorderLayout.CENTER);
		f.add(p,BorderLayout.SOUTH);
		p.add(l);
		p.add(t);
		p.add(b1);
		p.add(b2);
		ta.setEditable(false);
		try {
			ss=new ServerSocket(6666);
			ta.append("服务器已启动,等待客户端连接中...\n");
			s=ss.accept();
			ta.append("客户端连接完成,可传送文件\n");
			s.close();
			ss.close();
		} 
		catch(FileNotFoundException e1) {
			ta.append("系统找不到指定的路径!\n");
			e1.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws IOException {
		new MyFileServer();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Thread t1=new Thread(new Runnable() {
			public void run() {
				try {
					ss=new ServerSocket(6666);
					s=ss.accept();
				} 
				catch(FileNotFoundException e1) {
					ta.append("系统找不到指定的路径!\n");
					e1.printStackTrace();
				}
				catch (IOException e) {
					e.printStackTrace();
				}
					try {
						File file=new File(path);
						OutputStream out=s.getOutputStream();
						String filename=file.getName();
						out.write(filename.getBytes());
						out.flush();
						Thread.sleep(5000);
						BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));
						byte []buff=new byte[1024];
						int len;
						while((len=in.read(buff))!=-1){
							out.write(buff, 0, len);
						}
						ta.append("传送完成!可继续!(文件保存于D盘)\n");
						out.close();
						in.close();
						s.close();
						ss.close();
					}
					catch (InterruptedException e) {
						ta.append("文件传输中断!\n");
						e.printStackTrace();
					}
					catch (IOException e) {
						e.printStackTrace();
					}
			}	
		});
		String str=e.getActionCommand();
		if(str.equals("发送")) {
			path=t.getText();
			ta.append("正在传送文件:"+path+"\n");
			t.setText("");
			t1.start();
		}
		else if(str.equals("关闭")) {
			System.exit(0);
		}
	}
}

客户端:
package MyFileTCP;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.*;
import java.net.*;
public class MyFileClient implements ActionListener{
	String path;
	String filename;
	Socket s;
	JFrame f;
	JPanel p;
	JTextArea ta;
	JButton b1,b2;
	public MyFileClient(){
		f=new JFrame("客户端");
		p=new JPanel();
		ta=new JTextArea();
		b1=new JButton("接收");
		b1.addActionListener(this);
		b2=new JButton("关闭");
		b2.addActionListener(this);
		f.setVisible(true);
		f.setSize(360, 260);
		f.setLayout(new BorderLayout());
		f.add(ta,BorderLayout.CENTER);
		f.add(p,BorderLayout.SOUTH);
		p.add(b1);
		p.add(b2);
		ta.setEditable(false);
		try {
			s=new Socket("localhost",6666);
			ta.append("服务器连接成功,可接收文件!\n");
			s.close();
		} catch (UnknownHostException e) {
			ta.append("IP地址或端口号输入错误!\n");
			e.printStackTrace();
		}
		catch (ConnectException e2) {
			ta.append("找不到对应服务器!端口号输入错误!\n");
			e2.printStackTrace();
		}
		catch(SocketException e3) {
			ta.append("连接错误!\n");
			e3.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws UnknownHostException, IOException {
		new MyFileClient();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		Thread t1=new Thread(new Runnable() {
			public void run() {
				try {
					s=new Socket("localhost",6666);
				} catch (UnknownHostException e) {
					ta.append("IP地址或端口号输入错误!\n");
					e.printStackTrace();
				}
				catch (ConnectException e2) {
					ta.append("找不到对应服务器!端口号输入错误!\n");
					e2.printStackTrace();
				}
				catch(SocketException e3) {
					ta.append("连接错误!\n");
					e3.printStackTrace();
				}
				catch (IOException e) {
					e.printStackTrace();
				}
				
					try {
						InputStream in=s.getInputStream();
						byte []filenamebyte=new byte[1024];
						int filenamelen;
						filenamelen=in.read(filenamebyte);
						filename=new String(filenamebyte,0,filenamelen);
						path="d:/"+"copy--"+filename;
						ta.append("正在接收文件:"+"copy--"+filename+"\n");
						File file=new File(path);
						BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(file));
						byte[] buff=new byte[1024];
						int len;
						while((len=in.read(buff))!=-1) {
							out.write(buff, 0, len);
						}
						ta.append("接收完成!可继续!(文件保存于D盘)\n");
						out.close();
						in.close();
						s.close();
					}catch (IOException e) {
						e.printStackTrace();
					}
			}
		});
		String str=e.getActionCommand();
		if(str.equals("接收")) {
			t1.start();;
		}
		else if(str.equals("关闭")) {
			System.exit(0);
		}
	}
}

截图:


posted @ 2020-01-07 22:58  z全  阅读(97)  评论(0编辑  收藏  举报