本帖最后由 赵亚威 于 2013-4-3 18:58 编辑
package excercise;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Socket s=null;
try {
s=new Socket("127.0.0.1",10000);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//BufferedWriter bwOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
BufferedReader bwIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=br.readLine())!=null){
if("over".equals(line))
break;
pw.println(line);
String str=bwIn.readLine();
System.out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class SocketServer{
public static void main(String[] args){
ServerSocket ss=null;
Socket s=null;
try {
ss=new ServerSocket(10000);
s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println("ip:"+ip);
BufferedReader bwIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
//BufferedWriter bwOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
String line=null;
while((line=bwIn.readLine())!=null){
pw.println(line.toUpperCase());
//System.out.println();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
read()和readLine()以及write()阻塞方法 当输入中文时 无法返回 会卡屏或者卡死 这是神马情况 |
|