Java–网络编程–TCP实现聊天与文件上传
TCP聊天
服务端
package com.zy.net;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class StudyTcpServer {
public static void main(String[] args) {
initTcpServer();
}
public static void initTcpServer(){
ServerSocket serverSocket = null;
Socket accept = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
serverSocket = new ServerSocket(9999);
accept = serverSocket.accept();
is = accept.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (baos != null){
baos.close();
}
if (is != null){
is.close();
}
if (accept != null){
accept.close();
}
if (serverSocket != null){
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端
package com.zy.net;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class StudyTcpClient {
public static void main(String[] args) {
initTcpClient();
}
public static void initTcpClient(){
Socket socket = null;
OutputStream os = null;
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
socket = new Socket(serverIP,port);
os = socket.getOutputStream();
os.write("你好,这里是客户端".getBytes());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(os != null){
os.close();
}
if(socket != null){
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
TCP文件上传
服务端
package com.zy.net;
import javax.imageio.stream.FileImageOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class StudyTcpFileServer {
public static void main(String[] args) {
try {
initTcpFileServer();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void initTcpFileServer() throws Exception {
ServerSocket serverSocket = new ServerSocket(8888);
Socket accept = serverSocket.accept();
InputStream is = accept.getInputStream();
byte[] buffer = new byte[1024];
int len;
FileImageOutputStream fios = new FileImageOutputStream(new File("JavaProjectDemo/statics/netimg/receiveTwo.jpg"));
while((len = is.read(buffer)) != -1){
fios.write(buffer,0,len);
}
OutputStream os = accept.getOutputStream();
os.write("已接收完毕!".getBytes());
os.close();
fios.close();
is.close();
accept.close();
serverSocket.close();
}
}
客户端
package com.zy.net;
import javax.imageio.stream.FileImageInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class StudyTcpFileClient {
public static void main(String[] args) {
try {
initTcpFileClient();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void initTcpFileClient() throws Exception {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 8888;
Socket socket = new Socket(serverIP,port);
OutputStream os = socket.getOutputStream();
FileImageInputStream fiis = new FileImageInputStream(new File("JavaProjectDemo/statics/netimg/one.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = fiis.read(buffer)) != -1){
os.write(buffer,0,len);
}
socket.shutdownOutput();
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2 = is.read(buffer2)) != -1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
baos.close();
is.close();
fiis.close();
os.close();
socket.close();
}
}