关闭

java.net Socket

355人阅读 评论(0) 收藏 举报
import java.io.*; 
import java.net.*; 

public class Client 
{ 
Socket socket; 
BufferedReader in; 
PrintWriter out; 

public Client() 
{ 
try 
{ 
socket = new Socket("127.0.0.1", 10000); 
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

out = new PrintWriter(socket.getOutputStream(),true); 
BufferedReader line = new BufferedReader(new InputStreamReader(System.in)); 

out.println(line.readLine());
out.flush();
System.out.println(in.readLine());
//out.println("Hello Server!\n");
line.close(); 
out.close(); 

//
in.close(); 
socket.close(); 
} 
catch (IOException e) 
{} 
} 

public static void main(String[] args) 
{ 
new Client(); 
} 
} 


/*
 *Server: Main.java
 *
 * ServerDemo Java application
 *
 * Created on 29-03-2012 08:01 PM
 */
import java.net.*; 
import java.io.*; 

class Server 
{ 
private ServerSocket ss; 
private Socket socket; 
private BufferedReader in; 
private PrintWriter out; 

public Server() 
{ 
try 
{ 
ss = new ServerSocket(10000); 

while (true) 
{ 
socket = ss.accept(); 
System.out.println("建立新连接\n");
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
//out = new PrintWriter(socket.getOutputStream(),true);
out = new PrintWriter(socket.getOutputStream());

String line = in.readLine(); 
System.out.println("you input is :" + line); 
out.println("you input is :" + line); 
out.flush();
out.close(); 
in.close(); 
socket.close(); 
} 
//ss.close(); 
} 
catch (IOException e) 
{System.out.println(e.getMessage()); } 

} 
/*
public static void main(String[] args) 
{ 

} 
*/
} 

public class Main{
    public static void main(String[] args) {
		new Server(); 
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2990473次
    • 积分:40263
    • 等级:
    • 排名:第93名
    • 原创:399篇
    • 转载:4555篇
    • 译文:5篇
    • 评论:610条
    文章分类
    最新评论