package sock;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class MySock5 {
public static void main(String[] args) throws Exception {
FileInputStream inputStream = new FileInputStream("d:/美女.jpg");
byte[] arr = new byte[1234];
int res = 0;
Socket socket = new Socket("127.0.0.1", 12331);
OutputStream out = socket.getOutputStream();
while ((res = inputStream.read(arr)) != -1) {
out.write(arr);
}
socket.shutdownOutput();
InputStream in2 = socket.getInputStream();
byte[] bytes = new byte[1234];
int read = in2.read(bytes);
System.out.println(new String(bytes,0,read,"utf-8"));
inputStream.close();
socket.close();
}
}
class MySer5{
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(12331);
// byte[] bytes1 = "我是中文,你翻译看看".getBytes("utf-8");
// System.out.println(new String(bytes1));
while (true) {
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("d:/cop有美女.jpg");
byte[] bytes = new byte[1234];
int res = 0;
while ((res = inputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes);
}
OutputStream outputStream = accept.getOutputStream();
outputStream.write("我也得到美女了".getBytes());
fileOutputStream.close();
serverSocket.close();
}
}
}
005socket的基本使用
最新推荐文章于 2024-08-12 15:22:16 发布