protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} public class Server
extends Thread {
public final int Default_Port = 6544;
protected int port;
protected ServerSocket listen_socket;
protected Socket client;
protected DataInputStream in;
protected DataOutputStream out; // 定义出错例程:如果出现异常错误,退出程序。
public void fail(Exception e, String msg) {
System.err.println(msg + ": " + e);
System.exit(1);
} // 定义并启动服务器的Socket 例程,监听客户机的连接请求。
public Server(int port) {
if (port == 0) {
port = Default_Port;
}
this.port = port;
try {
listen_socket = new ServerSocket(port);
}
catch (IOException e) {
fail(e, "Exception creating server socket");
}
System.out.println("Server: listening on port" + port);
//this.start();
} /* 下面为服务器监听线程的主程序。该线程一直循环执行,监听并接受客户机发出的连接
请求。对每一个连接,均产生一个连接对象与之对应,通过Socket 通道进行通信。*/ public void run() {
try { while (true) {
Socket client_socket = listen_socket.accept();
client = client_socket;
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
String Message = null;
client.setSoTimeout(1000);
Message = in.readUTF();
System.out.print(Message);
// jTextArea2.setText(Message);
StringTokenizer st = new StringTokenizer(Message,"*");
label1.setText(st.nextToken());
while (st.hasMoreTokens()) {
list2.add(st.nextToken());
}
String ShareFiles ="";
for(int i=0;i<list1.getItemCount();i++)
{
ShareFiles = ShareFiles+list1.getItem(i)+"*";
}
System.out.print(ShareFiles);
//out.writeUTF(ShareFiles); out.close();
in.close();
client.close();
}
}
catch (IOException e) {
fail(e, "Exception while listening for connections");
}
}
// 启动服务器主程序
}// End of Server class
public class DownloadFile
extends Thread {
public final int Default_Port = 9999;
protected int port;
protected ServerSocket listen_socket;
protected Socket client;
protected DataInputStream in;
protected DataOutputStream out; // 定义出错例程:如果出现异常错误,退出程序。
public void fail(Exception e, String msg) {
System.err.println(msg + ": " + e);
System.exit(1);
} // 定义并启动服务器的Socket 例程,监听客户机的连接请求。
public DownloadFile(int port) {
if (port == 0) {
port = Default_Port;
}
this.port = port;
try {
listen_socket = new ServerSocket(port);
}
catch (IOException e) {
fail(e, "Exception creating server socket");
}
System.out.println("Server: listening on port" + port);
//this.start();
} /* 下面为服务器监听线程的主程序。该线程一直循环执行,监听并接受客户机发出的连接
请求。对每一个连接,均产生一个连接对象与之对应,通过Socket 通道进行通信。*/ public void run() {
try { while (true) {
Socket client_socket = listen_socket.accept();
client = client_socket;
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
String Message = null;
client.setSoTimeout(1000);
Message = in.readUTF();
//System.out.print(Message);
//jTextArea2.setText(Message);
StringTokenizer st = new StringTokenizer(Message," ");
Message = st.nextToken();
String fileName = Message;
File pictureFile = new File(fileName);
FileInputStream pictureInStream = new FileInputStream(pictureFile);
byte[] pictureData = new byte[4096];
int byteNumber;
while((byteNumber = pictureInStream.read(pictureData)) != -1)
out.write(pictureData,0,byteNumber);
//传送文件完成
pictureInStream.close();
out.close();
in.close();
client.close();
}
}
catch (IOException e) {
fail(e, "Exception while listening for connections");
}
}
// 启动服务器主程序
}
连接的时候出现错误
java.net.ConnectException: Connection refused: connect
我不怎么懂SOCKET,希望大家帮帮忙
谢谢
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} public class Server
extends Thread {
public final int Default_Port = 6544;
protected int port;
protected ServerSocket listen_socket;
protected Socket client;
protected DataInputStream in;
protected DataOutputStream out; // 定义出错例程:如果出现异常错误,退出程序。
public void fail(Exception e, String msg) {
System.err.println(msg + ": " + e);
System.exit(1);
} // 定义并启动服务器的Socket 例程,监听客户机的连接请求。
public Server(int port) {
if (port == 0) {
port = Default_Port;
}
this.port = port;
try {
listen_socket = new ServerSocket(port);
}
catch (IOException e) {
fail(e, "Exception creating server socket");
}
System.out.println("Server: listening on port" + port);
//this.start();
} /* 下面为服务器监听线程的主程序。该线程一直循环执行,监听并接受客户机发出的连接
请求。对每一个连接,均产生一个连接对象与之对应,通过Socket 通道进行通信。*/ public void run() {
try { while (true) {
Socket client_socket = listen_socket.accept();
client = client_socket;
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
String Message = null;
client.setSoTimeout(1000);
Message = in.readUTF();
System.out.print(Message);
// jTextArea2.setText(Message);
StringTokenizer st = new StringTokenizer(Message,"*");
label1.setText(st.nextToken());
while (st.hasMoreTokens()) {
list2.add(st.nextToken());
}
String ShareFiles ="";
for(int i=0;i<list1.getItemCount();i++)
{
ShareFiles = ShareFiles+list1.getItem(i)+"*";
}
System.out.print(ShareFiles);
//out.writeUTF(ShareFiles); out.close();
in.close();
client.close();
}
}
catch (IOException e) {
fail(e, "Exception while listening for connections");
}
}
// 启动服务器主程序
}// End of Server class
public class DownloadFile
extends Thread {
public final int Default_Port = 9999;
protected int port;
protected ServerSocket listen_socket;
protected Socket client;
protected DataInputStream in;
protected DataOutputStream out; // 定义出错例程:如果出现异常错误,退出程序。
public void fail(Exception e, String msg) {
System.err.println(msg + ": " + e);
System.exit(1);
} // 定义并启动服务器的Socket 例程,监听客户机的连接请求。
public DownloadFile(int port) {
if (port == 0) {
port = Default_Port;
}
this.port = port;
try {
listen_socket = new ServerSocket(port);
}
catch (IOException e) {
fail(e, "Exception creating server socket");
}
System.out.println("Server: listening on port" + port);
//this.start();
} /* 下面为服务器监听线程的主程序。该线程一直循环执行,监听并接受客户机发出的连接
请求。对每一个连接,均产生一个连接对象与之对应,通过Socket 通道进行通信。*/ public void run() {
try { while (true) {
Socket client_socket = listen_socket.accept();
client = client_socket;
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
String Message = null;
client.setSoTimeout(1000);
Message = in.readUTF();
//System.out.print(Message);
//jTextArea2.setText(Message);
StringTokenizer st = new StringTokenizer(Message," ");
Message = st.nextToken();
String fileName = Message;
File pictureFile = new File(fileName);
FileInputStream pictureInStream = new FileInputStream(pictureFile);
byte[] pictureData = new byte[4096];
int byteNumber;
while((byteNumber = pictureInStream.read(pictureData)) != -1)
out.write(pictureData,0,byteNumber);
//传送文件完成
pictureInStream.close();
out.close();
in.close();
client.close();
}
}
catch (IOException e) {
fail(e, "Exception while listening for connections");
}
}
// 启动服务器主程序
}
连接的时候出现错误
java.net.ConnectException: Connection refused: connect
我不怎么懂SOCKET,希望大家帮帮忙
谢谢
解决方案 »
- java.lang.NullPointerException ????
- 求救!!!Module null not found!
- 初学hashmap 几个问题
- 请问这两个正则表达式有什么区别?
- 请问actionlistener监听哪些事件?
- 请问大家怎样在一台计算机上得到另一台计算机的系统时间,急
- 怎样取得当前文件所在的路径?
- 大家帮我找找错误,是个算法问题
- 想用java实现点对点传输文件,大家有什么好的办法?
- 大家都学Java,可Java与C++哪个有钱途,本人正学C++,可又向学Java 该怎办?
- 请教 java 桌面程序 多语言支持
- 【新手提问】请问JTextField有没有清空内容的方法?
Server hustjgs = new Server(0);
hustjgs.start();
DownloadFile husthxl = new DownloadFile(0);
husthxl.start();
} void jButton3_actionPerformed(ActionEvent e) {
JFileChooser filechooser = new JFileChooser();
filechooser.setCurrentDirectory(new File("."));
filechooser.setFileSelectionMode(0);
int pp = filechooser.showOpenDialog(this); File file_list;
long lengthoffile; if (pp == 0) {
// selected the current file or directroy
file_list = filechooser.getSelectedFile();
// Find the length of the file
lengthoffile = file_list.length(); // Display the text on to the JTextField..
// String allfile = jTextArea3.getText();
//jTextArea3.setText(allfile + file_list.toString()+ "\n" );
list1.add(file_list.toString()+" "+lengthoffile+"K"); } } void jButton4_actionPerformed(ActionEvent e) {
JFileChooser filechooser = new JFileChooser();
filechooser.setCurrentDirectory(new File("."));
filechooser.setFileSelectionMode(1);
int pp = filechooser.showOpenDialog(this); File file_list;
long lengthoffile; if (pp == 0) {
// selected the current file or directroy
file_list = filechooser.getSelectedFile();
// Find the length of the file
lengthoffile = file_list.length(); label1.setText(file_list.toString());
jTextField1.setText(file_list.toString()); }
} void jButton6_actionPerformed(ActionEvent e) {
//list1.delItem(list1.getSelectedIndex());
int i= list1.getSelectedIndex();
label1.setText(Integer.toString(i));
//System.out.println(i);
list1.delItem(list1.getSelectedIndex());
list1.repaint();
//list1.remove(list1.getSelectedIndex());
//list1.remove(list1.getSelectedItem());
} void jButton5_actionPerformed(ActionEvent e) {
try {
String IPAdress = textField1.getText();
Socket clientSocket = new Socket(IPAdress, 6544);
String ip_address = null; // Store the ip address of local host
InetAddress localHostAddress = InetAddress.getLocalHost(); // local ip address converted into string
String local_address = localHostAddress.toString(); StringTokenizer st = new StringTokenizer(local_address, "/"); // While loop run till tokens are present
while(st.hasMoreTokens())
{
// login computer name stored in this variable
String machine_owner_name = st.nextToken(); // login computer ip address stored in this variable
ip_address = st.nextToken();
label1.setText(ip_address);
}
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream in = new DataInputStream(clientSocket.getInputStream()); String Message ="";
for(int i=0;i<list1.getItemCount();i++)
{
Message = Message+list1.getItem(i)+"*";
}
Message =ip_address +"*"+ Message;
out.writeUTF(Message);
in.close();
out.close();
clientSocket.close();
}
catch (Exception c) {
//System.out.println("Error");
c.printStackTrace();
} } void jButton2_actionPerformed(ActionEvent e) {
try {
String IPAdress = label1.getText();
Socket clientSocket = new Socket(IPAdress, 6544);
String ip_address = null; // Store the ip address of local host
InetAddress localHostAddress = InetAddress.getLocalHost(); // local ip address converted into string
String local_address = localHostAddress.toString(); StringTokenizer st = new StringTokenizer(local_address, "/"); // While loop run till tokens are present
while(st.hasMoreTokens())
{
// login computer name stored in this variable
String machine_owner_name = st.nextToken(); // login computer ip address stored in this variable
ip_address = st.nextToken();
//label1.setText(ip_address);
}
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream in = new DataInputStream(clientSocket.getInputStream()); String Message ="";
for(int i=0;i<list1.getItemCount();i++)
{
Message = Message+list1.getItem(i)+"*";
}
Message =ip_address +"*"+ Message;
out.writeUTF(Message);
in.close();
out.close();
clientSocket.close();
}
catch (Exception c) {
//System.out.println("Error");
c.printStackTrace();
} } void jButton7_actionPerformed(ActionEvent e) {
String DownloadFile = list2.getSelectedItem();
System.out.print(DownloadFile);
System.out.print(textField1.getText());
try {
Socket clientSocket = new Socket(textField1.getText(),9999);
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream in = new DataInputStream(clientSocket.getInputStream()); String fileName = DownloadFile;
out.writeUTF(fileName);
System.out.print("下载文件为:"+fileName+"\n"); StringTokenizer st = new StringTokenizer(fileName," ");
fileName = st.nextToken(); File pictureFile = new File(fileName);
if(pictureFile.exists())
System.out.println("This file" + fileName + " has existed!");
int i = 0;
while(pictureFile.exists()) {
fileName = fileName.substring(0,fileName.indexOf(".")) + i + fileName.substring(fileName.indexOf("."),fileName.length());
System.out.println(fileName);
pictureFile = new File(fileName);
i++;
}
//accept picture file;
FileOutputStream pictureOutStream = new FileOutputStream(pictureFile);
byte[] pictureData = new byte[4096];
int byteNumber;
while((byteNumber = in.read(pictureData)) != -1)
pictureOutStream.write(pictureData,0,byteNumber);
pictureOutStream.close(); in.close();
out.close();
clientSocket.close();
} catch(Exception d) {
//System.out.println("Error");
d.printStackTrace();
} }
}
Frame1 adaptee; Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}class Frame1_jButton3_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton3_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}class Frame1_jButton4_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton4_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton4_actionPerformed(e);
}
}class Frame1_jButton6_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton6_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton6_actionPerformed(e);
}
}class Frame1_jButton5_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton5_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton5_actionPerformed(e);
}
}class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}class Frame1_jButton7_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee; Frame1_jButton7_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton7_actionPerformed(e);
}
}
当你封装以后,经过测试,拿出来用,一般不会出问题,有问题也会健全你的基类。
以后你不管在哪里,最多改变一下你的通信协议,也就是组包的类改一下。
你就可以拿去用了,用在项目中省了很多事。