局域内的两台机子,怎么样通过应用程序来实现传送文件呢? 如题. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //server.javaimport java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Server extends JFrame { private JTextField enter; private JTextArea display; ObjectOutputStream output; ObjectInputStream input; public Server() { super("Server"); Container c =getContentPane(); enter=new JTextField(); enter.setEnabled(false); enter.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ sendData(e.getActionCommand()); } }); c.add(enter,BorderLayout.NORTH); display=new JTextArea(); c.add(new JScrollPane(display),BorderLayout.CENTER); setSize(300,150); show(); } public void runServer(){ ServerSocket server; Socket connection; int counter=1; try{ //第一步:create a ServerSocket. server=new ServerSocket(5000,100); while(true){ //第二步:wait for a connection. display.setText("等待连接\n"); connection=server.accept();display.append("connection"+counter+" received from: "+connection.getInetAddress().getHostName());//step 3:获得input和output 流output=new ObjectOutputStream(connection.getOutputStream());output.flush();input=new ObjectInputStream(connection.getInputStream());display.append("\n Got I/O stream \n");//step 4:连接过程.process connection.String message="SERVER>>> Connection successful";output.writeObject(message);output.flush();enter.setEnabled(true);do{try{message=(String) input.readObject();display.append("\n"+message);display.setCaretPosition(display.getText().length()); catch(ClassNotFoundException cnfex) { display.append("\n Unknown object type received"); } }while(!message.equals("CLIENT>>> TERMINATE")); //step 5:close connection. display.append("\n USER terminated connection"); enter.setEnabled(false); output.close(); input.close(); connection.close(); ++counter; } } catch(EOFException eof){ System.out.println("Client terminated connection"); } catch(IOException io){ io.printStackTrace(); } } private void sendData(String s){ try{ output.writeObject("SERVER>>>"+s); output.flush(); display.append("\nSERVER>>>"+S); } catch(IOException cnfex){ display.append("\nError writing object"); } } public static void main(String args[]){ Server app=new Server(); app.addWindowListener({ public void windowClosing(WindowEvent e){ System.exit(0); } }); app.runServer(); } }//client.javaimport java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Client extends JFrame{ private JTextField enter; private JTextArea display; ObjectOutputStream output; ObjectInputStream input; public Client(){ super("Client"); Container c =getContentPane(); enter=new JTextField(); enter.setEnabled(false); enter.addActionListener(new ActionListener(){ public void axtionPerformed(ActionEvent e){ sendData(e.getActionCommand()); } }); c.add(enter,BorderLayout.NORTH); display=new JTextArea(); c.add(new JScrollPane(display),BorderLayout.CENTER); setsize(300,150); show(); } public void runClient(){ Socket client; try{ //step1:create a socket connection. display.setText("Attempting connection\n"); client=new Socket(InetAddress.getByName("127.0.0.1"),5000); display.append("connected to :"+client.getInetAddress().getHostName()); //step 2:get the input and output streams. output=new ObjectOutputStream(client.getOutputStream()); output.flush(); input=new ObjectInputStream(client.getInputStream()); display.append("\n GOT I/O streams\n"); //step 3:process connection. enter.setEnabled(true); do{ try{ message=(String) input.readObject(); display.append("\n"+message); display.setCaretPosition(display.getText().length()); }catch( ClassNotFoundException cnfex ){ display.append("\nUnknown object type received"); } }while(!message.equals("SERVER>>>TERMINATE")); display.append("Closing connection.\n"); input.close(); output.close(); client.close(); } catch(EOFException eof){ System.out.println("Server terminated connection"); } catch(IOException e){ e.printStackTrace(); } } private void sendData(String s){ try{ message=s; output.writeObject("CLIENT>>>"+s); output.flush(); display.append("\nCLIENT>>>"+s); }catch(IOException cnfex){ display.append("\nError writing object"); } } public static void main(String args[]){ Client app=new Client(); app.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); app.runClient(); } }这是我想做一个局域网内的两点间聊天程序,假如我想加一个传输文件,我改怎么样做呢? JTable的最大容量(行数)一般是多少? JAVA一个小程序的问题 jdbc中Statement.createStatement()的问题ResultSet.TYPE_SCROLL_INSENSITIVE和ResultSet.TYPE_SCORLL_SENSITIVE的区别 读字符串 容器的选择: 系统上线讨论 自己做的,遇到问题特来请教!!!!! 一个日期计算问题 一个关于tag的使用问题(救急) 文件尾缀为.001这样的数字式的怎么看? Java能做什么?Java不能做什么? 一个很奇怪的问题 高手新手都请进
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame {
private JTextField enter;
private JTextArea display;
ObjectOutputStream output;
ObjectInputStream input;
public Server()
{
super("Server");
Container c =getContentPane();
enter=new JTextField();
enter.setEnabled(false);
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendData(e.getActionCommand());
}
});
c.add(enter,BorderLayout.NORTH);
display=new JTextArea();
c.add(new JScrollPane(display),BorderLayout.CENTER);
setSize(300,150);
show();
}
public void runServer(){
ServerSocket server;
Socket connection;
int counter=1;
try{
//第一步:create a ServerSocket.
server=new ServerSocket(5000,100);
while(true){
//第二步:wait for a connection.
display.setText("等待连接\n");
connection=server.accept();
display.append("connection"+counter+" received from: "+connection.getInetAddress().getHostName());
//step 3:获得input和output 流
output=new ObjectOutputStream(connection.getOutputStream());
output.flush();
input=new ObjectInputStream(connection.getInputStream());
display.append("\n Got I/O stream \n");
//step 4:连接过程.process connection.
String message="SERVER>>> Connection successful";
output.writeObject(message);
output.flush();
enter.setEnabled(true);
do{
try{
message=(String) input.readObject();
display.append("\n"+message);
display.setCaretPosition(display.getText().length());
catch(ClassNotFoundException cnfex) {
display.append("\n Unknown object type received");
}
}while(!message.equals("CLIENT>>> TERMINATE"));
//step 5:close connection.
display.append("\n USER terminated connection");
enter.setEnabled(false);
output.close();
input.close();
connection.close();
++counter;
}
}
catch(EOFException eof){
System.out.println("Client terminated connection");
}
catch(IOException io){
io.printStackTrace();
}
}
private void sendData(String s){
try{
output.writeObject("SERVER>>>"+s);
output.flush();
display.append("\nSERVER>>>"+S);
}
catch(IOException cnfex){
display.append("\nError writing object");
}
}
public static void main(String args[]){
Server app=new Server();
app.addWindowListener({
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
app.runServer();
}
}//client.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame{
private JTextField enter;
private JTextArea display;
ObjectOutputStream output;
ObjectInputStream input;
public Client(){
super("Client");
Container c =getContentPane();
enter=new JTextField();
enter.setEnabled(false);
enter.addActionListener(new ActionListener(){
public void axtionPerformed(ActionEvent e){
sendData(e.getActionCommand());
}
});
c.add(enter,BorderLayout.NORTH);
display=new JTextArea();
c.add(new JScrollPane(display),BorderLayout.CENTER);
setsize(300,150);
show();
}
public void runClient(){
Socket client;
try{
//step1:create a socket connection.
display.setText("Attempting connection\n");
client=new Socket(InetAddress.getByName("127.0.0.1"),5000);
display.append("connected to :"+client.getInetAddress().getHostName());
//step 2:get the input and output streams.
output=new ObjectOutputStream(client.getOutputStream());
output.flush();
input=new ObjectInputStream(client.getInputStream());
display.append("\n GOT I/O streams\n");
//step 3:process connection.
enter.setEnabled(true);
do{
try{
message=(String) input.readObject();
display.append("\n"+message);
display.setCaretPosition(display.getText().length());
}catch( ClassNotFoundException cnfex ){
display.append("\nUnknown object type received");
}
}while(!message.equals("SERVER>>>TERMINATE"));
display.append("Closing connection.\n");
input.close();
output.close();
client.close();
}
catch(EOFException eof){
System.out.println("Server terminated connection");
}
catch(IOException e){
e.printStackTrace();
}
}
private void sendData(String s){
try{
message=s;
output.writeObject("CLIENT>>>"+s);
output.flush();
display.append("\nCLIENT>>>"+s);
}catch(IOException cnfex){
display.append("\nError writing object");
}
}
public static void main(String args[]){
Client app=new Client();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
app.runClient();
}
}
这是我想做一个局域网内的两点间聊天程序,假如我想加一个传输文件,我改怎么样做呢?