socket

服务器端代码

package cn.java.net.TCP.infoExchange;

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

public class Server {

private static int count;
private static Socket socket;

public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = new ServerSocket(9527);
    System.out.println("服务器已启动,等待客户连接...");
    while(true){
        socket = serverSocket.accept();
        ServerThread st = new ServerThread(socket);
        st.start();
        System.out.println("这是第"+(count++)+"位用户");
        System.out.println("此用户的IP地址为"+socket.getInetAddress().getHostAddress());
    }
}

}

服务器处理函数:

package cn.java.net.TCP.infoExchange;

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

public class ServerThread extends Thread {

private Socket socket = null;
private InputStream inputStream = null;
private InputStreamReader inputStreamReader = null;
private BufferedReader bufferedReader = null;
private OutputStream outputStream = null;
private PrintStream printStream = null;
private String a;
private String b = "地瓜地瓜,我是土豆";

public ServerThread(Socket socket) {
    this.socket = socket;
}

@Override
public void run() {
    super.run();
    try {
        inputStream = socket.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
    inputStreamReader = new InputStreamReader(inputStream);
    bufferedReader = new BufferedReader(inputStreamReader);
    try {
        while((a = bufferedReader.readLine()) != null){
            System.out.println("客户端说:"+a);
            socket.shutdownInput();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        outputStream = socket.getOutputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
    printStream = new PrintStream(outputStream);
    try {
        printStream.write(b.getBytes());
        socket.shutdownOutput();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

客户端代码:

package cn.java.net.TCP.infoExchange;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Cilent {

private static String info = " 土豆土豆,我是地瓜";
private static String localhost;
private static byte[] b;
private static String a;

public static void main(String[] args) throws UnknownHostException, IOException {
    Socket socket = new Socket(localhost, 9527);
    OutputStream outputStream = socket.getOutputStream();
    PrintStream ps = new PrintStream(outputStream);
    b = info.getBytes();
    ps.write(b);
    ps.flush();
    //千万不能忘记关闭输入输出流!!否则不会出结果!!
    socket.shutdownOutput();

    InputStream inputStream = socket.getInputStream();
    BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
    while((a = bf.readLine()) != null){
        System.out.println("服务器说:"+a);
    }
    socket.shutdownInput();

    bf.close();
    inputStream.close();
    ps.close();
    outputStream.close();
    socket.close();
}

}

注意事项:应先启动服务器端在启动客户端,两者在各自的控制台分别输出对应的打印语句.在Eclipse中按照下图切换:

以上代码参考自

http://blog.csdn.net/qq_23473123/article/details/51461894

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值