Java中TCP的简单使用
来源:互联网 发布:js li取消隐藏 编辑:程序博客网 时间:2024/06/02 02:49
服务器端:
import java.io.*;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;/** * Created by cuboo on 2016/10/12. */public class intnet_server { private ServerSocket serverSocket; private Socket socket; private InputStreamReader reader; private OutputStreamWriter writer; public static void main(String agrs[]){ getHostInfo(); intnet_server server = new intnet_server(); server.getServer(); } public static void getHostInfo(){ InetAddress ip; try { ip = InetAddress.getLocalHost(); String name = ip.getHostName(); String localip = ip.getHostAddress(); System.out.println("本机名:"+name); System.out.println("本机IP地址:"+localip); } catch (UnknownHostException e) { e.printStackTrace(); } } public void getServer(){ try { serverSocket = new ServerSocket(8888,10,InetAddress.getLocalHost()); System.out.println("服务器套接字创建成功!"); int i = 0; while (true){ System.out.println("等待连接..."+i); i++; socket = serverSocket.accept(); getClientInfo(); sentMessage(); } } catch (IOException e) { e.printStackTrace(); } } public void getClientInfo(){ try { DataInputStream dis = new DataInputStream(socket.getInputStream()); System.out.println(dis.readUTF());// reader = new InputStreamReader(socket.getInputStream());// char[] chars = new char[100];// reader.read(chars);// System.out.println(new String(chars,0,chars.length)); } catch (IOException e) { e.printStackTrace(); } } public void sentMessage(){ try { DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeUTF("server"); dos.flush();// writer = new OutputStreamWriter(socket.getOutputStream());// writer.write("server");// writer.flush(); } catch (IOException e) { e.printStackTrace(); } }}
客服端:
import java.io.*;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;/** * Created by cuboo on 2016/10/12. */public class client { private Socket socket; private InputStreamReader reader; private OutputStreamWriter writer; private DataOutputStream bos; private DataInputStream dis; public static void main(String agrs[]){ getHostInfo(); client c = new client(); c.myClient(); c.recieveMessage(); } public void myClient(){ try { socket = new Socket("ipv4.cuboo.cn",8888); bos = new DataOutputStream(socket.getOutputStream()); bos.writeUTF("client"); bos.flush();// writer = new OutputStreamWriter(socket.getOutputStream());// writer.write("client");// writer.flush(); } catch (IOException e) { e.printStackTrace(); } } public static void getHostInfo(){ InetAddress ip; try { ip = InetAddress.getLocalHost(); String name = ip.getHostName(); String localip = ip.getHostAddress(); System.out.println("本机名:"+name); System.out.println("本机IP地址:"+localip); } catch (UnknownHostException e) { e.printStackTrace(); } } public void recieveMessage(){ try { dis = new DataInputStream(socket.getInputStream()); System.out.println(dis.readUTF());// reader = new InputStreamReader(socket.getInputStream());// char[] chars = new char[100];// reader.read(chars);// System.out.println(new String(chars,0,chars.length)); } catch (IOException e) { e.printStackTrace(); } }}
0 0
- Java中TCP的简单使用
- Java中使用TCP协议的简单连接
- 最简单的java中TCP的网络编程
- JAVA中tcp/ip和UDP的使用
- Java语言中包的简单使用
- Java中正则表达式的简单使用
- java中Vector的简单使用
- Java中Hashtable的简单使用
- java中使用简单的filter
- Java中File的简单使用
- JAVA中JNI的简单使用
- Java中Hashtable的简单使用
- java中map的简单使用
- java中AES加密的简单使用
- java中枚举类型的简单使用
- Java中Map的简单使用
- JAVA中注解的简单使用
- java中runtime的简单使用
- Java集合---ConcurrentHashMap原理分析
- 微信成长
- HDU-1847-Good Luck in CET-4 Everybody!【巴什博弈】
- 基于MySQL游标的具体使用详解
- easyUI中index的使用
- Java中TCP的简单使用
- Unity5 新AssetBundle打包方式 BuildPipeline.BuildAssetBundles
- 求斐波那契数的栈模拟递归算法
- PHP:检查代理ip的有效性
- bat文件详解
- Java集合---面试题
- Dom解析器使用实例
- iOS大神读源码
- strust_入门教程