码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA TCP Socket

时间:2014-09-17 08:56:01      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   java   ar   div   art   

服务器端

 

package com.Pong.tcpip;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer {

    //先启动服务器端程序
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=new ServerSocket(8080);
        Socket socket=serverSocket.accept();//阻塞等待消息
        InputStream inputStream=socket.getInputStream();
        OutputStream outputStream=socket.getOutputStream();
//        outputStream.write("欢迎光临!".getBytes(), 0,"欢迎光临!".getBytes().length );
        outputStream.write("welcome!".getBytes() );
    /*    byte[] buf=new byte[1024];
        int len=inputStream.read(buf);
//        System.out.println(new String(buf, 0, len));
        inputStream.close();*/
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
        System.out.println(bufferedReader.readLine());
        bufferedReader.close();
        outputStream.close();
        socket.close();
        serverSocket.close();
    }
}
package com.Pong.tcpip;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Servicer implements Runnable {
    Socket socket;

    public Servicer(Socket socket) {
        super();
        this.socket = socket;
    }

    @Override
    public void run() {
    try {
        InputStream inputStream=socket.getInputStream();
        OutputStream outputStream=socket.getOutputStream();
        
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
        DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
        while (true) {
            String strWord=bufferedReader.readLine();
//            System.out.println(strWord+":"+strWord.length());
            if ("quit".equalsIgnoreCase(strWord)) {
                break;
            }
            String strEcho=(new StringBuilder(strWord).reverse()).toString();
            //dataOutputStream.writeBytes(strWord+"---->"+strEcho+"\r\n");
            //System.getProperty("line.separator")为跨平台换行符。Window为\r\n,unix为\n
            dataOutputStream.writeBytes(strWord+"---->"+strEcho+System.getProperty("line.separator"));
        }
        bufferedReader.close();
        bufferedReader.close();
        socket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    }
    
    public static void main(String [] args)
    {
        try
        {
            ServerSocket ss=new ServerSocket(8080);
            while(true)
            {
                Socket s=ss.accept();
                new Thread(new Servicer(s)).start();
            }
            //ss.close();
        }catch(Exception e){e.printStackTrace();}
    }

}

客户端

package com.Pong.tcpip;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
//后启动客户端程序
public class TcpClient {
    //启动main时传进来两个参数:ip和端口号
    public static void main(String [] args) 
    {
    try
    {
        //Socket s=new Socket(InetAddress.getByName("192.168.0.213"),8001);
        if(args.length < 2)
        {
            System.out.println("Usage:java TcpClient ServerIP ServerPort");
            return;
        }
        //建立Socket
        Socket s=new Socket(InetAddress.getByName(args[0]),Integer.parseInt(args[1]));
        InputStream ips=s.getInputStream();
        OutputStream ops=s.getOutputStream();
        
        BufferedReader brKey = new BufferedReader(new InputStreamReader(System.in));//键盘输入
        DataOutputStream dos = new DataOutputStream(ops);
        BufferedReader brNet = new BufferedReader(new InputStreamReader(ips));

        while(true)
        {
            String strWord = brKey.readLine();
            dos.writeBytes(strWord + System.getProperty("line.separator"));
            if(strWord.equalsIgnoreCase("quit"))
                break;
            else
                System.out.println(brNet.readLine());
        }
        dos.close();
        brNet.close();
        brKey.close();
        s.close();
    }catch(Exception e){e.printStackTrace();}
    }
}
/*发送和接受都是以Socket为信息载体*/

JAVA TCP Socket

标签:style   blog   color   io   os   java   ar   div   art   

原文地址:http://www.cnblogs.com/Kconnie/p/3976353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!