客户端:

package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException; public class Client {
public static void main(String[] args) {
Socket s = null;
InputStream is = null;
BufferedReader br = null; try {
s = new Socket("127.0.0.1", 6666);
is = s.getInputStream();
br = new BufferedReader(new InputStreamReader(is)); String str = br.readLine();
while(str!=null){
System.out.println(str);
str = br.readLine();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}

服务端:

package socket;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket; public class Server {
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
OutputStream os = null;
BufferedWriter bw = null; try {
ss = new ServerSocket(6666);
s = ss.accept();
os = s.getOutputStream();
bw = new BufferedWriter(new OutputStreamWriter(os)); bw.write("hello java !");
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}

最新文章

  1. php5.6+apache2.4环境配置
  2. 用Python向MySQL数据库插入数据
  3. Dynamics AX 2012 R2 为运行失败的批处理任务设置预警
  4. memset的使用
  5. 性能优化-查询最耗CPU的SESSION与SQL
  6. 函数rec_get_nth_field_offs_old
  7. IP地址变动后,https://localhost:1158/em无法访问解决办法
  8. Eclipse 完全 设置UTF-8 环境
  9. rsyslog ~ 波浪号
  10. MongoDB学习(翻译4)
  11. 关于云Linux部署tomcat服务器(Maven的多模块war包)
  12. java中String和StringBuffer的区别
  13. 文件操作 day8
  14. SVN Hooks的介绍及使用
  15. liferay笑傲江湖-API之参数的工具类(ParamUtil)
  16. Source引擎多人模式网络同步模型
  17. nodejs 接收上传的图片
  18. JS实现幸运抽奖页面
  19. 8.初识Lock与AbstractQueuedSynchronizer(AQS)
  20. LeetCode 621. Task Scheduler

热门文章

  1. HTML5学习笔记 Web存储
  2. Linux下源码安装JDK7
  3. c# 高效的线程安全队列ConcurrentQueue
  4. selenium 实现浏览器 前进和后退
  5. action(一)
  6. 聊一聊HTML <pre>标签
  7. dependent-name ‘xxx::yyy’ is parsed as a non-type, but instantiation yields a type
  8. PHP之文件大小的转换函数
  9. Android——RelativeLayout(相对布局)
  10. josn 格式 解析