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

java之socket

时间:2016-05-13 17:20:43      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

socket系java中网络编程的内容。

1客户端:

package com.wtd.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPClint {
    public static void main(String[] args) {
        try {
            Socket clint= new Socket("127.0.0.1", 9000);
            PrintWriter pWriter = new PrintWriter(clint.getOutputStream());
            pWriter.println("hello world");
            pWriter.flush();
            BufferedReader bReader= new BufferedReader(new InputStreamReader(clint.getInputStream()));
            String line= bReader.readLine();
            System.out.println(line);
            clint.close();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

2.服务器:

服务器线程:

package com.wtd.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerThread extends Thread {
    private Socket socket;
    public ServerThread(Socket socket) {
        this.socket= socket;
        // TODO Auto-generated constructor stub
    }
    @Override
    public void run() {
        BufferedReader bReader;
        try {
            bReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String text = bReader.readLine();
            PrintWriter pWriter = new PrintWriter(socket.getOutputStream());
            pWriter.println(text+ " from thread server");
            pWriter.flush();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } finally{
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    }
}

2.服务器端:

package com.wtd.socket;

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

public class TCPServer2 {
    public static void main(String[] args) {
        try {
            ServerSocket server= new ServerSocket(9000);
            while (true) {
                Socket socket= server.accept();
                ServerThread serverThread= new ServerThread(socket);
                serverThread.start();
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

执行:先执行服务器端,然后执行客户端:

output:

hello world from thread server

java之socket

标签:

原文地址:http://www.cnblogs.com/westward/p/5489958.html

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