这几天学习了socket的一些知识,尝试下写点程序。下面就是一个简单的文件传输程序,目前的功能也就是让服务器传输文件到达客户端而已,。这个类是用来封转socket的一些操作
这是服务端,默认就传送指定文件夹中的文件:
import java.net.*;
import java.io.*;
/**
* 该类封装socket的发送,接收等操作,
* @author Administrator
*
*/
public class ConnectionSocket extends Socket{
private Socket socket;
private BufferedReader input;
private PrintWriter output;
public ConnectionSocket(String acceptorHost, int acceptorPort)throws SocketException,IOException{
socket = new Socket(acceptorHost,acceptorPort);
setStreams();
}
public ConnectionSocket(Socket socket)throws IOException{
this.socket = socket;
setStreams();
}
private void setStreams() throws IOException{
InputStream instream = socket.getInputStream();
input = new BufferedReader(new InputStreamReader(instream));
OutputStream outstream = socket.getOutputStream();
output = new PrintWriter(new OutputStreamWriter(outstream));
}
public void sendMessage(String message)throws IOException{
output.println(message);
output.flush();
}
public String receiveMessage()throws IOException{
String message = input.readLine();
return message;
}
}
这是服务端,默认就传送指定文件夹中的文件:
import java.io.*; import java.net.*; /** * 这是一个socket编程的服务器端,暂时具有发送文件功能 * @author Administrator * */ public class ConnectionServe { public static void main(String args[]){ int portNo = 8080; String filePath = "inputfile//025.png"; filePath = "inputfile//hello.txt"; File file = new File(filePath); try{ ServerSocket connectionSocket = new ServerSocket(portNo); connectionSocket.setSoTimeout(10000); System.out.println("ready connection"); //开始传送文件 ConnectionSocket dataSocket = new ConnectionSocket(connectionSocket.accept()); //发送文件名 dataSocket.sendMessage(file.getName()); //读取文件内容并发送 //这厮使用行来读取的,如果是txt等文件还好,但是对于图像文件则不能正确传送 /** BufferedReader in = new BufferedReader(new FileReader(file)); String message = null; while((message = in.readLine()) != null){ dataSocket.sendMessage(message); } **/ //这个是字节读取,用于图像等传送 InputStream in = new FileInputStream(file); int tempbyte; while ((tempbyte = in.read()) != -1) { dataSocket.sendMessage(String.valueOf(tempbyte)); } //关闭各种流 in.close(); dataSocket.close(); connectionSocket.close(); }catch (IOException e){ e.printStackTrace(); } } }
这是客户端,就接受文件把文件保存在指定的文件夹中:import java.io.*; import java.net.*; /** * 这是socket程序中的客户端,暂时的功能有接收文件 * @author Administrator * */ public class ConnetctionClient { public static void main(String args[]){ String acceptorHost = "localhost"; int acceptorPort = 8080; String message = null; // BufferedWriter out = null; OutputStream out =null; ConnectionSocket connection = null; try { connection = new ConnectionSocket(acceptorHost, acceptorPort); System.out.println("connection request granted"); System.out.println("message begin to receive"); //第一个接收到的是文件名 message = connection.receiveMessage(); message= "outputfile\\"+message; new File(message).createNewFile(); System.out.println(message); //文件流,覆盖原文件 //这是按行写入文件的 /** out = new BufferedWriter(new FileWriter(message)); while((message = connection.receiveMessage())!=null){ System.out.println(message); out.write(message); out.newLine(); } **/ //这是一个写图像文件的方法 out = new FileOutputStream(message); int tempbyte; while ((tempbyte = Integer.parseInt(connection.receiveMessage()))!=-1) { // System.out.println(tempbyte); out.write(tempbyte); } } catch (SocketException e) { System.out.println("socket has ended!"); } catch (IOException e) { e.printStackTrace(); }finally{ //关闭流 try { out.close(); connection.close(); } catch (IOException e1) { e1.printStackTrace(); } } } }
其实,java socket编程的原理也是简单很多,server端需要使用java提供的server以及socketserver。而client端只需要使用java提供的server而已。写起来方便很多了,不需要像C++那么复杂。第一次写博客,有什么不足的地方还请见谅了!!!!![]()