java.net Socket

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(); 
    }
}

阅读更多
个人分类: Java笔记
上一篇str2num
下一篇const IID IID_IUnknown
想对作者说点什么? 我来说一句

java.netsocket通信

2009年09月29日 1003B 下载

没有更多推荐了,返回首页

关闭
关闭