Java TCP客户端服务器端交互
2016-07-24 08:51
483 查看
package com.knightsight.chatroom; import java.net.*; import java.io.*; /* * 输入流与输出流独立,客户端程序 */ public class Client { public static void main(String[] args) throws IOException,UnknownHostException{ Socket client =new Socket("localhost",9900); //控制台输入流 BufferedReader console=new BufferedReader(new InputStreamReader(System.in)); DataOutputStream dos=new DataOutputStream(client.getOutputStream()); DataInputStream dis=new DataInputStream(client.getInputStream()); while(true){ String info=console.readLine(); //输出流 dos.writeUTF(info); dos.flush(); //输入流 String str=dis.readUTF(); System.out.println(str); } } }
package com.knightsight.chatroom;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(9900);
Socket socket=server.accept();
DataInputStream dis=new DataInputStream(socket.getInputStream());
String str=dis.readUTF();
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
dos.writeUTF("服务器已收到"+str);
dos.flush();
}
}
相关文章推荐
- 源码推荐(0724B):网络数据安全--base64 和 MD5 的简单封装,视频播放器封装AVPlayer
- 逐步深入TCP/IP协议栈
- centos 链接不到网络
- Android三种基本的加载网络图片方式
- Ubuntu 16.04更新软件提示"需要安装不能信任的软件包" http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease
- TCP
- 饭卡 (背包01 一维数组) http://acm.hdu.edu.cn/showproblem.php?pid=2546
- Socket —— TCP 初识
- tomcat配置https ssl
- commons-httpclient和htmlparser应用之博客搬家
- 主机字节序与网络字节序
- VirtualBox NAT网络模式 端口映射
- http protocol 协议
- TCP 的那些事儿(上)
- 安卓开发之网络请求HttpURLConnection
- netty对http协议解析原理(一)
- RSA(施工中)
- Tcpdump
- http返回码
- TCP协议通讯原理