-
java tcp socket实例
2011-04-20 13:58 2364人阅读 收藏 举报
- package com.network;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.InetSocketAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Arrays;
-
-
- public class ServerChat {
-
-
-
-
- public static void main(String[] args) throws IOException {
-
-
- InetSocketAddress isa = new InetSocketAddress("10.140.86.98",5858);
-
- ServerSocket server = new ServerSocket();
-
- server.bind(isa);
- System.out.println("isBound: " + server.isBound());
- System.out.println("SocketAddress: " + server.getLocalSocketAddress());
-
- new Thread(new WorkingServer(server)).start();
-
- while(true){
- Socket client = server.accept();
- System.out.println("this main thread");
-
- InputStream in = client.getInputStream();
-
- InputStreamReader read = new InputStreamReader(new BufferedInputStream(in));
-
- OutputStream out = client.getOutputStream();
-
- OutputStreamWriter write = new OutputStreamWriter( new BufferedOutputStream(out));
-
- write.write("hello");
- write.flush();
-
- char [] cbuf = new char[100];
- Arrays.fill(cbuf, '/0');
- int len = read.read(cbuf, 0, 100);
- StringBuilder sb = new StringBuilder(100);
- sb.append(cbuf, 0, len);
-
- System.out.println(sb.toString());
-
- read.close();
- client.close();
- write.close();
-
- }
-
-
-
-
-
-
- }
- }
- package com.network;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.Arrays;
- public class WorkingServer implements Runnable {
- private ServerSocket server;
-
- WorkingServer(ServerSocket s){
- server = s;
- }
-
- void communicat( Socket client) throws IOException{
- System.out.println("this sub thread: " + Thread.currentThread().getId());
-
- InputStream in = client.getInputStream();
-
- InputStreamReader read = new InputStreamReader(new BufferedInputStream(in));
-
- OutputStream out = client.getOutputStream();
-
- OutputStreamWriter write = new OutputStreamWriter( new BufferedOutputStream(out));
-
- write.write("hello");
- write.flush();
-
- char [] cbuf = new char[100];
- Arrays.fill(cbuf, '/0');
- int len = read.read(cbuf, 0, 100);
- StringBuilder sb = new StringBuilder(100);
- sb.append(cbuf, 0, len);
-
- System.out.println(sb.toString());
-
- read.close();
-
- write.close();
-
- }
-
- @Override
- public void run() {
-
- System.out.println(Thread.currentThread());
- while(true){
- try {
- Socket client = server.accept();
- Thread.sleep(5000);
- communicat(client);
- } catch (IOException e) {
-
- e.printStackTrace();
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- }
- }
- }
- package com.network;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class ClientSock {
-
-
-
-
-
- public static void main(String[] args) throws UnknownHostException, IOException {
-
- Socket s = new Socket("10.140.86.98", 5858);
-
- System.out.println( "remote socket " + s.getRemoteSocketAddress());
-
- InputStream in = s.getInputStream();
-
- InputStreamReader reader = new InputStreamReader(in);
-
- char [] cbuf = new char[100];
- int len = reader.read(cbuf);
- StringBuilder sb = new StringBuilder(100);
-
- sb.append(cbuf, 0, len);
- System.out.println(sb.toString());
-
- OutputStreamWriter writer = new OutputStreamWriter(s.getOutputStream());
-
- writer.write("from client");
-
- writer.close();
- reader.close();
- s.close();
-
- }
- }
life is a jounery,yes
-
相关阅读:
查看hbase中的中文
查看hbase中的中文
scala使用hbase新api
scala使用hbase新api
IDEA15使用maven编译scala和java
IDEA15使用maven编译scala和java
IDEA非sbt下spark开发
IDEA非sbt下spark开发
sed初学者实用说明
sed初学者实用说明
-
原文地址:https://www.cnblogs.com/CaptainLin/p/3596536.html
Copyright © 2020-2023
润新知