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) {
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 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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fis.close();
dos.close();
socket.close();
} catch (IOException e) {
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("请输入你的学号 班级 姓名 ");
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 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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fis.close();
dos.close();
socket.close();
} catch (IOException e) {
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();
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");
}
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
{
jtaChat.append("你的退出时间是:"+"\n");
jtaChat.append(sdf.format(new Date())+"\n");
}
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);
while(true)
{
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
int port = s.getPort();
new Thread(new ServerThread(s, ss, ip, port)).start();
}
}
}
class ServerThread implements Runnable
{
Socket s = null;
ServerSocket ss = null;
String ip = null;
int port = 0;
String uid = null;
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) {
e.printStackTrace();
}
}
}
}