62,614
社区成员
发帖
与我相关
我的任务
分享
// 服务器端
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(12345);
System.out.println("The server is started, please start the client");
boolean flag = true;
byte[] buff = new byte[1024];
while( flag ) {
Socket so = s.accept();
int read = so.getInputStream().read(buff);
String ss = new String(buff,0,read);
if( ss.equals("end"))
so.getOutputStream().write("end".getBytes());
so.getOutputStream().write("respose".getBytes());
so.getOutputStream().flush();
so.close();
}
s.close();
}
}
//客户端
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("please input the length of (3,4,5)");
String ipaddr = "localhost";
Socket so = new Socket(ipaddr,12345);
boolean flag = true;
while ( flag ) {
System.out.println("Please input something here");
byte[] buff=new byte[64];
int r = System.in.read(buff);
so.getOutputStream().write(new String(buff,0,r).getBytes());
System.out.println("we got back from Server");
r = so.getInputStream().read(buff); //这行出错了
String str = new String(buff,0,r);
System.out.print( str );
if( str.equals("end"))
flag = false;
}
so.close();
}
}