通过客户端键盘录入单词,并发送到服务端。
服务端在控制台打印后,将该单词转成大写再发回客户端。
服务器端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Receive {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10057);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+".....connected");
String str= null;
String str2 = null;
InputStream in = s.getInputStream();
int len =0;
byte[] buf = new byte[1024];
while((len= in.read(buf))!=-1){
str=new String(buf,0,len);
str2=str.toUpperCase();
System.out.println(str2);
}
in.close();
System.out.println("'aaaaasdfdsfsdfsd"); //为什么从这里开始后面都不执行?求指教
OutputStream out = s.getOutputStream();
System.out.println("'sdfdsfsdfsd");
out.write(str2.getBytes());
s.close();
}
}
客户端
public class Send {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 10057);
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入信息:");
OutputStream out = socket.getOutputStream();
String line;
while((line=buf.readLine())!=null){
byte[] arr = line.getBytes();
out.write(arr);
}
InputStream in = socket.getInputStream();
int len =0;
byte[] bi = new byte[1024];
while((len= in.read(bi))!=-1){
System.out.println(new String(bi,0,len));
}
socket.close();
}
}
|
|