bio-test
作者:互联网
package com.luban.bio;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class BioClient {
public static void main(String[] args) {
try {
Socket socket=new Socket("127.0.0.1",6789);
new Thread(){
@Override
public void run() {
while (true){
try {
byte[] b=new byte[1024];
int read = socket.getInputStream().read(b);
if(read>0){
System.out.println(new String(b));
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}.start();
while (true){
Scanner scanner=new Scanner(System.in);
while(scanner.hasNextLine()){
String s = scanner.nextLine();
socket.getOutputStream().write(s .getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.luban.bio;
import com.luban.utils.HttpUtil;
import sun.util.calendar.BaseCalendar;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class BioServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket=new ServerSocket(6789);
while (true){
Socket socket = serverSocket.accept();
new Thread(){
@Override
public void run() {
while (true){
try {
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
System.out.println(new String(bytes));
OutputStream outputStream = socket.getOutputStream();
outputStream.write(HttpUtil.getHttpResponseContext200(new SimpleDateFormat("yyyy-MM-dd hh:dd:ss").format(new Date())).getBytes("utf-8"));
outputStream.flush();
socket.close();
break;
}catch (Exception e){
e.printStackTrace();
}
}
}
}.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
标签:bio,java,socket,import,test,new,public,String 来源: https://www.cnblogs.com/mfk11/p/16251997.html