/*
需求:通过Tcp传输,将客户端输入的字符传给服务器端并打印成大写 返回给客户端显示
*/
import java.net.*;
import java.io.*;
class ClientDemo2
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.100",10003);
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//PrintWriter out=new PrintWriter(s.getOutputStream(),true);简化书写
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while ((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
//out.println(line);
bufOut.write(line);
bufOut.newLine();
bufOut.flush();
String str=bufIn.readLine();
System.out.println(str);
}
bufr.close();
s.close();
}
}
class ServerDemo2
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10003);
Socket s=ss.accept();
BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bufOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"...connection");
String line=null;
while ((line=bufIn.readLine())!=null)
{
bufOut.write(line.toUpperCase());
bufOut.newLine();
bufOut.flush();
}
s.close();
ss.close();
}
}
|
|