Java

package a;

import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyServer4 {
	
		 public static void main(String[] args){
			 DataInputStream dis=null;
			 Socket socket =null;
			 FileOutputStream fos =null;
			 InputStreamReader ir = null;
			 BufferedReader br = null;
			 try{
			    int length=0;
			    byte[] getByte = new byte[1024];		
			    ServerSocket ss = new ServerSocket(7777);
			    System.out.println("服务器创建完毕");
			    socket = ss.accept();
			    ir=new InputStreamReader(socket.getInputStream());
			    br=new BufferedReader(ir);			
			    System.out.println("连接到客户端");
			    dis = new DataInputStream(socket.getInputStream());
			    File file = new File("C:\\Users\\lenovo\\Desktop\\2");
			    fos = new FileOutputStream(file);		
		            String first = br.readLine();
		            String second = br.readLine();        
		            System.out.println(first);
		            System.out.println(second);						
	                    System.out.println("准备接收文件");
				while((length=dis.read(getByte))>0){				
					fos.write(getByte, 0, length);
					fos.flush();
				}
		            System.out.println("文件接收完毕");	       	        	        
			 }catch(IOException e){
				 e.getStackTrace();
			 }finally{
				 try {
					dis.close();
					fos.close();
				        socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			 }		 
		 }	 	 
	

}

package a;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.table.*;

//import b.Client1;

//import b.ClientFrame;

import javax.swing.*;
public class MyClient4 {
	  
		 public static void main(String[] args){
			 ClientFrame cframe = new ClientFrame(); 
			 cframe.setVisible(true);
		int length =0;
		FileInputStream fis = null;
	        DataOutputStream dos = null;
	        Socket socket = null; 
	        OutputStream out =null;
	        PrintWriter pw = null;
	        byte[] sendByte = null;
			 try {
				socket = new Socket("localhost",7777);
				out = socket.getOutputStream();
				pw = new PrintWriter(out);
				System.out.println("连接到服务器成功");
				File file = new File("C:\\Users\\lenovo\\Desktop\\1.txt");
				fis = new FileInputStream(file);
				dos = new DataOutputStream(socket.getOutputStream());
				sendByte = new byte[1024];			
				pw.write("123"+"\r\n");
				pw.flush();
				pw.write("456"+"\r\n");
				pw.flush();			
				System.out.println("准备发送");
				while((length=fis.read(sendByte))>0){				
					dos.write(sendByte, 0 , length);
					dos.flush();
				}
				System.out.println("文件发送完毕");						
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					fis.close();
					dos.close();
				        socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}		 		 
		 }		
	

}

class ClientFrame extends JFrame
{
	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	 final int WIDTH = 700;
	 final int HEIGHT = 700;
	 JButton btnSendfile = new JButton("发送文件");
	 JButton btnLogin = new JButton("学生登录");
	 JButton btnExit = new JButton("退出");
	  JTextArea jtaSay = new JTextArea();
	  JTextArea jtaShow = new JTextArea();
	  JLabel lblReceiver = new JLabel("请输入你的学号 班级 姓名  ");
	 // JTextArea jtaChat = new JTextArea();
	  String[][] rowData = null;
	  public ClientFrame()
	  {
		  setTitle("学生管理");
		  setSize(WIDTH, HEIGHT);
		  setLayout(null);
		  btnLogin.setBounds(20, 600, 160, 60);
	        btnSendfile.setBounds(180, 600, 160, 60);
	        btnExit.setBounds(340, 600, 160, 60);
	      
	    
	        lblReceiver.setBounds(20, 420, 300, 30);
	        btnLogin.setFont(new Font("宋体", Font.BOLD, 18));
	        btnSendfile.setFont(new Font("宋体", Font.BOLD, 18));
	        JScrollPane jspShow= new JScrollPane(jtaShow);
	        btnExit.setFont(new Font("宋体", Font.BOLD, 18));
	        jtaSay.setBounds(20, 460, 360, 120);
	        //设置文本输入框字体
	        jtaSay.setFont(new Font("楷体", Font.BOLD, 16));
	        this.add(jspShow);
	        this.add(jtaSay);
	        this.add(btnLogin);
	        this.add(btnSendfile);
	      
	        this.add(btnExit);
	        this.add(lblReceiver);
	        btnLogin.addActionListener
            (
                 new ActionListener()
                 {
                    @Override
                    public void actionPerformed(ActionEvent event)
                    {
                    	try{
                        jtaShow.append(sdf.format(new Date()));
                        jtaShow.append(jtaSay.getText() + "\n\n");
                    	}
                    	  catch(Exception e){}
                    	
                    }
                 }
            );
	        btnExit.addActionListener
            (
                 new ActionListener()
                 {
                    @Override
                    public void actionPerformed(ActionEvent event)
                    {
                        
                    }
                 }
            );
	  }
	  
}


package a;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.table.*;
//import b.Client1;

//import b.Client1;

//import b.ClientFrame;

import javax.swing.*;
public class MyClient4 {
	  
		 public static void main(String[] args){
			 ClientFrame cframe = new ClientFrame(); 
			 cframe.setVisible(true);
		int length =0;
		FileInputStream fis = null;
	        DataOutputStream dos = null;
	        Socket socket = null; 
	        OutputStream out =null;
	        PrintWriter pw = null;
	        byte[] sendByte = null;
			 try {
				socket = new Socket("localhost",7777);
				out = socket.getOutputStream();
				pw = new PrintWriter(out);
				System.out.println("连接到服务器成功");
				
				File file = new File("C:\\Users\\asus\\Desktop\\1.txt");
				fis = new FileInputStream(file);
				dos = new DataOutputStream(socket.getOutputStream());
				sendByte = new byte[1024];			
				pw.write("123"+"\r\n");
				pw.flush();
				pw.write("456"+"\r\n");
				pw.flush();			
				System.out.println("准备发送");
				while((length=fis.read(sendByte))>0){				
					dos.write(sendByte, 0 , length);
					dos.flush();
				}
				System.out.println("文件发送完毕");						
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					fis.close();
					dos.close();
				        socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}		 		 
		 }		
	

}
class ClientFrame extends JFrame
{
    //时间显示格式
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 
    //窗口宽度
    final int WIDTH = 700;
    //窗口高度
    final int HEIGHT = 700;
    
    //创建发送按钮
    JButton btnSend = new JButton("发送");
    //创建清除按钮
    JButton btnLogin = new JButton("登录");
    //创建退出按钮
    JButton btnExit = new JButton("退出");
    //创建消息接收者标签
    JLabel lblReceiver = new JLabel("请输出你的学号,姓名,班级");
 
    //创建文本输入框, 参数分别为行数和列数
    JTextArea jtaSay = new JTextArea();
 
    //创建聊天消息框
    JTextArea jtaChat = new JTextArea();
 
    //当前在线列表的列标题
   // String[] colTitles = { "网名", "IP", "端口"};
    //当前在线列表的数据
   // String[][] rowData = null;
    //创建当前在线列表
   
    
    //创建聊天消息框的滚动窗
    JScrollPane jspChat = new JScrollPane(jtaChat);
    //设置默认窗口属性,连接窗口组件
    public ClientFrame()
    {    
        //标题
        setTitle("学生管理");
        //大小
        setSize(WIDTH, HEIGHT );
        //不可缩放
        setResizable(false);
        //设置布局:不适用默认布局,完全自定义
        setLayout(null);
        //设置按钮大小和位置
        btnSend.setBounds(20, 600, 100, 60);
        btnLogin.setBounds(140, 600, 100, 60);
        btnExit.setBounds(260, 600, 100, 60);
 
        //设置标签大小和位置
        lblReceiver.setBounds(20, 420, 300, 30);
 
        //设置按钮文本的字体
        btnSend.setFont(new Font("宋体", Font.BOLD, 18));
        btnLogin.setFont(new Font("宋体", Font.BOLD, 18));
        btnExit.setFont(new Font("宋体", Font.BOLD, 18));
 
        //添加按钮
        this.add(btnSend);
        this.add(btnLogin);
        this.add(btnExit);
        //添加标签
        this.add(lblReceiver);
        //设置文本输入框大小和位置
        jtaSay.setBounds(20, 460, 360, 120);
        //设置文本输入框字体
        jtaSay.setFont(new Font("楷体", Font.BOLD, 16));
        //添加文本输入框
        this.add(jtaSay);
        
        //聊天消息框自动换行
        jtaChat.setLineWrap(true);
        //聊天框不可编辑,只用来显示
        jtaChat.setEditable(false);
        //设置聊天框字体
        jtaChat.setFont(new Font("楷体", Font.BOLD, 16));
 
        //设置滚动窗的水平滚动条属性:不出现
       jspChat.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        //设置滚动窗的垂直滚动条属性:需要时自动出现
        jspChat.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        //设置滚动窗大小和位置
        jspChat.setBounds(20, 20, 360, 400);
        //添加聊天窗口的滚动窗
        this.add(jspChat);

       
     
       
        btnLogin.addActionListener
                                (
                                     new ActionListener()
                                     {
                                        @Override
                                        public void actionPerformed(ActionEvent event)
                                        {
                                            
                                            try
                                            {
                                            	  jtaChat.append("你的登陆时间是"+ "\n");
                                                    jtaChat.append(sdf.format(new Date())+"\n");
                                                    jtaChat.append("如果你完成了实验,请将文件放在桌面,并命名为1"+ "\n");
                                                    //显示发送消息
                                                    jtaChat.append(jtaSay.getText() + "\n\n");
                                                    //向服务器发送聊天信息
                                                   // OutputStream out = Client1.s.getOutputStream();
                                                   // out.write(("Chat/" + Client1.uidReceiver.toString() + "/" + jtaSay.getText()).getBytes());
                                               // } 
                                            }
                                            catch(Exception e){}
                                            finally
                                            {
                                                //文本输入框清除
                                                jtaSay.setText("");
                                            }
                                        }
                                     }
                                );
       
        btnSend.addActionListener
                                (
                                     new ActionListener()
                                     {
                                        @Override
                                        public void actionPerformed(ActionEvent event)
                                        {
                                            //聊天框清屏
                                            jtaChat.setText("");
                                        }
                                     }
                                );
        //添加退出按钮的响应事件
        btnExit.addActionListener
                                (
                                     new ActionListener()
                                     {
                                        @Override
                                        public void actionPerformed(ActionEvent event)
                                        {
                                            try
                                            {
                                                //向服务器发送退出信息
                                               // OutputStream out = Client1.s.getOutputStream();
                                              //  out.write("Exit/".getBytes());
                                                //退出
                                            	 
                                            	 jtaChat.append("你的退出时间是:"+"\n");
                                            	 jtaChat.append(sdf.format(new Date())+"\n");
                                              //  System.exit(0);
                                            }
                                            catch(Exception e){}
                                        }
                                     }
                                );
        
    }
}

package a;


import java.io.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.*;

public class MyServer4 {
	
		 public static void main(String[] args) throws Exception {
			 ServerSocket ss = new ServerSocket(7777);
			 System.out.println("Server online... " + ss.getInetAddress().getLocalHost().getHostAddress() + ", " +7777);
		        //监听端口,建立连接并开启新的ServerThread线程来服务此连接
		        while(true)
		        {
		            //接收客户端Socket
		            Socket s = ss.accept();
		            //提取客户端IP和端口
		            String ip = s.getInetAddress().getHostAddress();
		            int port = s.getPort();
		            //建立新的服务器线程, 向该线程提供服务器ServerSocket,客户端Socket,客户端IP和端口
		            new Thread(new ServerThread(s, ss, ip, port)).start();
		        }
		
		 }	 	 
	

}

class ServerThread implements Runnable 
{
	
	 Socket s = null;
	    //获取的服务器ServerSocket
	    ServerSocket ss = null;
	    //获取的客户端IP
	    String ip = null;
	    //获取的客户端端口
	    int port = 0;
	    //组合客户端的ip和端口字符串得到uid字符串
	    String uid = null;
	    
	    //静态ArrayList存储所有uid,uid由ip和端口字符串拼接而成
	   // static ArrayList<String> uid_arr = new ArrayList<String>();
	    //静态HashMap存储所有uid, ServerThread对象组成的对
	   // static HashMap<String, ServerThread> hm = new HashMap<String, ServerThread>();
	    
	    public ServerThread(Socket s, ServerSocket ss, String ip, int port)
	    {
	        this.s = s;
	        this.ss = ss;
	        this.ip = ip;
	        this.port = port;
	        uid = ip + ":" + port;
	    }
	    public void run() {
	 DataInputStream dis=null;
	 Socket socket =null;
	 FileOutputStream fos =null;
	 InputStreamReader ir = null;
	 BufferedReader br = null;
	 try{
	    int length=0;
	    byte[] getByte = new byte[1024];		
	    ServerSocket ss = new ServerSocket(7777);
	    System.out.println("服务器创建完毕");
	    socket = ss.accept();
	    ir=new InputStreamReader(socket.getInputStream());
	    br=new BufferedReader(ir);			
	    System.out.println("连接到客户端");
	    dis = new DataInputStream(socket.getInputStream());
	    File file = new File("C:\\Users\\asus\\Desktop\\2");
	    fos = new FileOutputStream(file);		
            String first = br.readLine();
            String second = br.readLine();        
            System.out.println(first);
            System.out.println(second);						
                System.out.println("准备接收文件");
		while((length=dis.read(getByte))>0){				
			fos.write(getByte, 0, length);
			fos.flush();
		}
            System.out.println("文件接收完毕");	       	        	        
	 }catch(IOException e){
		 e.getStackTrace();
	 }finally{
		 try {
			dis.close();
			fos.close();
		        socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	 }	
	    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值