求java通过socket传送文件的例子(300分)

  • 主题发起人 主题发起人 netspur
  • 开始时间 开始时间
1. Server program source
// Server.java
/**
* This a Socket Server program
* @version 1.0 1999/12/27
* @author Huang Jian-chang
**/
import java.net.*;
import java.io.*;
public class Server
{
ServerSocket server;
DataOutputStream output;
Socket socket;
public Server (){
try{
// create a server on port 5000
server=new ServerSocket(5000);
// display interactive informaion
System.out.println("Server created.");
System.out.println("waiting for client to connect on...");
// waiting for client to connect on...
socket = server.accept();
// client connected
System.out.println("client connected./nShutdown!");

output = new DataOutputStream(socket.getOutputStream());
output.writeUTF("Welcome to Server.Bye!");
output.close();
server.close();
}
catch(SocketException e){
System.out.println(e.toString());
e.printStackTrace();
System.exit(1);
}
catch(IOException e){
System.out.println(e.toString());
e.printStackTrace();
System.exit(1);
}
}
public static void main(String args[]){
Server game=new Server();
}
}

2. Application Client program source
// Client.java
/**
* This a Socket Client program
* @version 1.0 1999/12/27
* @author Huang Jian-chang
**/
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) {
try{
if (args.length != 1){
System.out.println("USAGE: java Client servername");
return;
}
String connectto= args[0];
Socket connection;
// connect to server
if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),5000);
}
else
{
connection=new Socket(InetAddress.getByName(connectto),5000);
}
DataInputStream input=new DataInputStream(connection.getInputStream());

// read information from server
String info;
info = input.readUTF();
System.out.println(info);
connection.close();

}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
}
catch(IOException e){
System.out.println("IOException when connecting Server!");

}
}
}
 
我有关于Socket编程的说明,给个email,我发给你。
 
我想j2se的文档中就有源码和tutorial。
JavaTM Secure Socket Extension (JSSE)
Reference Guide
for the JavaTM 2 SDK, Standard Edition, v 1.4
如file:///C:/JavaSoft/j2sdk1.4.1_01/docs/guide/security/jsse
 
to only you:你给的例子只是传送字符串的例子啊。
to henryandpcw:我的email是netspur@163.net
 
已给你发去,请查收。
 
多人接受答案了。
 
后退
顶部