JAVA信息传送代码之格式转换
package xin.week1.day6;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
//客户端给服务端发送文本,服务端将文本转成大写再返回给客户端
public class tcpshangzhuanhuan {
@Test
public void server() throws IOException {
ServerSocket ss = new ServerSocket(8848);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
byte[] bytes = new byte[1024];
int len;
String str = "";
while ((len = is.read(bytes)) != -1) {
str = new String(bytes, 0, len).toUpperCase() + str;
}
System.out.println("接受成功");
os.write(str.getBytes());
System.out.println("发送成功");
is.close();
os.close();
socket.close();
ss.close();
}
@Test public void client() throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8848);
OutputStream os = socket.getOutputStream();
//上传文本内容
FileInputStream fis = new FileInputStream("E:\\lj\\qwe.txt");
//下载内容
FileOutputStream fos = new FileOutputStream("E:\\lj\\asd.txt");
byte[] bytes = new byte[1024];
int len;
while ((len= fis.read(bytes))!=-1) {
os.write(bytes,0,len);
System.out.println(new String(bytes));
}
socket.shutdownOutput();
InputStream is =socket.getInputStream();
int len1;
while ((len1= is.read(bytes))!=-1) {
fos.write(bytes,0,len1);
}
System.out.println("发送的文本转换成功");
is.close();
fos.close();
fis.close();
os.close();
socket.close();
}
}
在这里欢迎大家的点赞、关注、评论,以此来促进大家互相学习交流,同时可以让新加入的小伙伴更快的了解新知识!!!
文章内容如有侵权,请联系作者进行删除
≧◠◡◠≦ 1分2分都是爱,感谢已经打赏的老板,和正在打赏的老板们 ≧◠◡◠≦