socket示例代码演示程序(螺纹)

client码,如以下:

import java.io.*;
import java.net.*;
public class DailyAdviceClient {
	public void go(){
			
		try {
			Socket s=new Socket("127.0.0.1",4242);//建立对server的Socket连接
			//client从server读取数据
			//建立连接到socket上低层输入串流的InputStreamReader
			//InputStreamReader是低层和高层串流间的桥梁
			//s.getInputStream()从Socket取得输入串流
			InputStreamReader streamReader=new InputStreamReader(s.getInputStream());
			//建立BufferedReader来读取
			BufferedReader reader=new BufferedReader(streamReader);
			String adviceString=reader.readLine();
			System.out.println("Today you should: "+adviceString);	
			//client向server发送数据	
			//创建不带自己主动行刷新的PrintWriter对象。
			//假设加入第二个參数true,则为自己主动刷新,不须要写writer.flush()这行代码	
			PrintWriter writer=new PrintWriter(s.getOutputStream());
			writer.println("Hello,I am Client2!");
			writer.flush();//刷新发送数据
			System.out.println("此套接字连接的远程地址:"+s.getInetAddress().getHostAddress());
			System.out.println("此套接字绑定的本地地址:"+s.getLocalAddress().getHostAddress());
			System.out.println("此套接字绑定的本地端口:"+s.getLocalPort());
			System.out.println("此套接字连接到的远程端口:"+s.getPort());
			s.close();//关闭socket
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void main(String[]args){
		DailyAdviceClient client=new DailyAdviceClient();
		client.go();
	}
}
server端代码例如以下:

import java.io.*;
import java.net.*;
public class DailyAdviceServer {
	String[] adviceList={"Take smaller bites","One word: inappropriate",""
			+ "Just for today,be honest","Tell your boss"};
	public void go(){
		
		try {
			//服务器应用程序对特定port创建出ServerSocket对象
			//这会让服务器应用程序监听来自4242port的client请求
			ServerSocket serverSocket=new ServerSocket(4242);
			//服务器进入无穷循环等待client的请求
			while(true){
				System.out.println("等待client连接...");
				//接收到用户连接请求后。此方法会返回一个socket。以便于client通信
				//此socket的port号和serverSocket的port号不同
				Socket sock=serverSocket.accept();//此方法会堵塞直到有连接请求到达
				System.out.println("此套接字连接的地址:"+sock.getInetAddress().getHostAddress());
				System.out.println("此套接字绑定的本地地址:"+sock.getLocalAddress().getHostAddress());
				System.out.println("此套接字绑定的本地port:"+sock.getLocalAddress());
				System.out.println("此套接字连接到的远程port:"+sock.getPort());
				//服务器向client发送数据
				PrintWriter writer=new PrintWriter(sock.getOutputStream());
				String adviceString=getAdvice();
				writer.println(adviceString);
				writer.flush();
				System.out.println(adviceString);
				//服务器接收client发送的数据
				InputStreamReader streamReader=new InputStreamReader(sock.getInputStream());
				BufferedReader reader=new BufferedReader(streamReader);
				String adviceStringREC=reader.readLine();
				System.out.println("REC: "+adviceStringREC);			

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public String getAdvice(){
		int random=(int)(Math.random()*adviceList.length);
		return adviceList[random];
		
	}
	public static void main(String []args){
		DailyAdviceServer server=new DailyAdviceServer();
		server.go();
	}
}


版权声明:本文博客原创文章。博客,未经同意,不得转载。

posted @ 2015-08-11 11:03  yxwkaifa  阅读(217)  评论(0编辑  收藏  举报