java socket(TCP)学习记录

/ 2014-09-17

        最近在 学习了socket 发现socket果然是个好东西. 仔细的去了解发现用途真的太多.通过scoket编写的东西能很好的做到各种兼容性.比如大家孰知的HTTP 协议了.sokcet通过一些同行协议就能就完成了HTTP . 想想就感觉好厉害呀.

        HTTP 这样强大的协议也不是在任何情况都是很有效的.如HTTP 需要实现长链接就显得有些力不从心了.HTTP 适合BS下的应用需要什么数据就从服务器请求什么数据.如果想要服务器主动找客户端的话就不是那样容易了.HTTP 基于请求的特点就无法完成服务端一主动找客户端(浏览器)

HTTP 协议是BS 的应用需要的.为了满足服务端的需要求.请求头是比较长的. 于是实际的业务逻辑中是需要自己去实现一些协议.

        编写一个简单的服务端的客户端的代码

        server 端代码

  

package com.fzb.socket;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer {

	public static void main(String[] args) {
		ServerSocket server=null;
		try {
			server=new ServerSocket(8888);
			System.out.println("server is run");
			while(true){
				// accept 这个方式会对server进行堵塞 当有链接上来后会不再堵塞
				Socket socket=server.accept();
				System.out.println(socket.getInetAddress() +" get connect");
				socket.getOutputStream().write(("Hello I'm server,you want to tell some sth "+MyClient.ENDCHAR).getBytes());
				InputStream in=socket.getInputStream();
				byte sth[]=MyClient.getByteArrayByInputStream(in);
				System.out.println("client say: "+new String(sth));
				socket.getOutputStream().write(("now I know, you say "+new String(sth)+MyClient.ENDCHAR).getBytes() );
				in.close();
				socket.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			if(server!=null){
				try {
					server.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


        client 端代码


package com.fzb.socket;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class MyClient {

	public static String ENDCHAR="*";
	public static void main(String[] args) {
		try {
			Socket socket=new Socket("127.0.0.1", 8888);
			InputStream in=socket.getInputStream();
			String message=new String(getByteArrayByInputStream(in));
			System.out.println("server say: "+message);
			Scanner sc=new Scanner(System.in);
			String sendMessage=sc.nextLine()+ENDCHAR;
			OutputStream out=socket.getOutputStream();
			out.write(sendMessage.getBytes());
			// 读取服务端的消息
			message=new String(getByteArrayByInputStream(in));
			System.out.println("server say: "+message);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static byte[] getByteArrayByInputStream(InputStream in){
		byte temp[]=new byte[5];
		ByteArrayOutputStream out=new ByteArrayOutputStream();
		try {
			// in.read(temp) 这里也会发生堵塞.有到来是不再发生堵塞
			in.read(temp);
			while(!new String(temp).trim().endsWith(ENDCHAR)){
				out.write(temp);
				temp =new byte[5];
				in.read(temp);
			}
			out.write(new String(temp).replace(ENDCHAR, "").getBytes());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toByteArray();
	}
}


client console

server console


Reproduced please indicate the author and the source, and error a link to this page.
text link: //xiaochun.zrlog.com/161.html