日期:2014-05-20 浏览次数:20842 次
ServerSocket ss = null;
try{
ss = new ServerSocket(2222);
while(true){
Socket socket = ss.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("客户端说:"+reader.readLine());
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(ss!=null)
ss.close();
}catch (Exception e) {
}
}
Socket socket = null;
try{
socket = new Socket("127.0.0.1", 2222);
System.out.println("连接已建立");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while(true){
writer.write("我是客户端..\\r\\n");
writer.flush();
Thread.sleep(3000);
System.out.println("发送完毕...");
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
}
}
ss = new ServerSocket(1212);
Socket socket = ss.accept();//放在外面
while (true) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
System.out.println("客户端说:" + reader.readLine());
}
while (true) {
writer.write("我是客户端..\\r\\n");
writer.newLine();//加上这个:写入一个行分隔符
writer.flush();
Thread.sleep(3000);
System.out.println("发送完毕...");
}
import java.net.*;
import java.io.*;
import java.util.*;
public class Server {
public static void main(String[] args){
ServerSocket ss = null;
try{
ss = new ServerSocket(2222);