本文主要简单介绍了下使用socket进行简单的以字符串为形式的一对一回话和一对一的文件上传。
话不多说,代码如下:
服务端代码:
package com.qsding.socket.server;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import com.qsding.socket.util.Util;
/**
* desc: Socket 服务端<br/>
*
* @author qsding <br/>
* @date 2018年4月2日-下午11:22:01 <br/>
*/
public class Server {
public static void main(String[] args) {
Server server = new Server();
// server.dealString();
server.dealFile();
}
/**
* 使用socket处理传输文件
*/
public void dealFile() {
ServerSocket serverSocket = null;
Socket clientSocket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
FileOutputStream fos = null;
try {
// 创建服务端
serverSocket = new ServerSocket(Util.PORT);
// 阻塞线程获取客户端
clientSocket = serverSocket.accept();
// 获取输入流
inputStream = clientSocket.getInputStream();
File file = new File("copyFile.txt");
if(!file.exists()){
file.createNewFile();
}
fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int len = 0;
while((len = inputStream.read(b))!=-1){
fos.write(b, 0, len);
}
// 获取输出流
outputStream = clientSocket.getOutputStream();
String osStr = "文件上传完毕。服务端保存地址:"+file.getAbsolutePath();
outputStream.write(osStr.getBytes(Util.UTF_8));
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fos) {
fos.close();
}
if (null != outputStream) {
outputStream.close();
}
if (null != inputStream) {
inputStream.close();
}
if (null != clientSocket) {
clientSocket.close();
}
if (null != serverSocket) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 用socket处理字符串
*/
public void dealString() {
ServerSocket serverSocket = null;
Socket clientSocket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
// 创建服务端
serverSocket = new ServerSocket(Util.PORT);
// 阻塞线程获取客户端
clientSocket = serverSocket.accept();
// 获取输入流
inputStream = clientSocket.getInputStream();
String inputStreamData = Util.getInputStreamData(inputStream);
System.out.println(inputStreamData);
// 获取输出流
outputStream = clientSocket.getOutputStream();
String osStr = "好好学习,天天向上!!!";
outputStream.write(osStr.getBytes(Util.UTF_8));
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != outputStream) {
outputStream.close();
}
if (null != inputStream) {
inputStream.close();
}
if (null != clientSocket) {
clientSocket.close();
}
if (null != serverSocket) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端代码:
package com.qsding.socket.client;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import com.qsding.socket.util.Util;
/**
* desc: Socket客户端<br/>
*
* @author qsding <br/>
* @date 2018年4月2日-下午11:22:16 <br/>
*/
public class Client {
public static void main(String[] args) {
Client client = new Client();
// client.dealString();
client.dealFile();
}
/**
* 使用socket处理传输文件
*/
public void dealFile() {
Socket socket = null;
OutputStream outputStream = null;
InputStream inputStream = null;
FileInputStream fis = null;
try {
// 创建客户端
socket = new Socket(InetAddress.getLocalHost(), Util.PORT);
// 获取输出流
outputStream = socket.getOutputStream();
// 创建文件对象
File file = new File("C:/Users/qsding/Desktop/Untitled 2.txt");
// 传输文件名
outputStream.write(file.getName().getBytes(Util.UTF_8));
// 立刻写出(此处可深究,写不是立刻就写出去了的,具体可查阅其他相关资料)
// outputStream.flush();
// 获取文件输入流
fis = new FileInputStream(file);
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b)) != -1) {
outputStream.write(b, 0, len);
}
// 关闭输出流
socket.shutdownOutput();
// 获取输入流
inputStream = socket.getInputStream();
String inputStreamData = Util.getInputStreamData(inputStream);
System.out.println(inputStreamData);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fis) {
fis.close();
}
if (null != inputStream) {
inputStream.close();
}
if (null != outputStream) {
outputStream.close();
}
if (null != socket) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 用socket处理字符串
*/
public void dealString() {
Socket socket = null;
OutputStream outputStream = null;
InputStream inputStream = null;
try {
// 创建客户端
socket = new Socket(InetAddress.getLocalHost(), Util.PORT);
String osStr = "==> 服务端,我发送消息来咯哟...";
// 获取输出流
outputStream = socket.getOutputStream();
outputStream.write(osStr.getBytes(Util.UTF_8));
// 关闭输出流
socket.shutdownOutput();
// 获取输入流
inputStream = socket.getInputStream();
String inputStreamData = Util.getInputStreamData(inputStream);
System.out.println(inputStreamData);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != inputStream) {
inputStream.close();
}
if (null != outputStream) {
outputStream.close();
}
if (null != socket) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
工具类(还可将读写的输出输出剥离出来,放入工具类中):
package com.qsding.socket.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* desc: 工具类<br/>
* @author qsding <br/>
* @date 2018年4月2日-下午11:22:20 <br/>
*/
public final class Util {
/** 端口号 */
public static final int PORT = 8080;
/** 编码 UTF-8 */
public static final String UTF_8 = "UTF-8";
/**
* 私有化构造器
*/
private Util(){
}
/**
* 获取输入流中字符串解析
* @param inputStream 输入流
* @return 字符串
* @throws IOException
*/
public static String getInputStreamData(InputStream inputStream) throws IOException {
StringBuilder sb = new StringBuilder("");
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,UTF_8));
String str = "";
while(null!=(str = br.readLine())){
sb.append(str).append("\r\n");
}
return sb.toString();
}
}