以下是跟毕老师敲的,但是还是报异常等待,看了半天不知道哪里出错了,请大神忙找一找!谢谢!
import java.io.*;
import java.net.*;
class TransClient {
public static void main(String [] args) throws Exception
{
Socket s =new Socket("192.168.1.137",10005);
//定义读取键盘数据的流对象。
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//定义目的,将数据写入到socket输入流,发给服务端。
// 用下面简化方法BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
//定义一个socket读取流,读取服务端返回的大写信息。
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while((line = bufr.readLine())!= null)
{
if("over".equals(line))
break;
out.print(line);
// bufOut.write(line);
//bufOut.newLine();//结束标记!**
//bufOut.flush();
String str = bufIn.readLine();
System.out.print("server:"+str);
}
bufr.close();
s.close();
}
}
/*
服务端:
源:socket读取流。
目的:socket输出流,
都是文本,装饰。
*/
class TransServer
{
public static void main(String [] args) throws Exception
{
ServerSocket ss = new ServerSocket(10005);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"...connected");
//读取socket读取流中的数据。
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
//目的,socket输出流,将大写数据写入到socket输出流,并发送客户端。
//用下面的简化方法BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
while ((line=bufIn.readLine())!=null)
{
System.out.println(line);
out.println(line.toUpperCase());
//用上面简化方法bufOut.write(line.toUpperCase());
//bufOut.newLine();//结束标记!**
//bufOut.flush();
}
s.close();
ss.close();
}
}
|
|