Socket通信编程实例(SIB和SS'SOB)
2024-08-28 00:10:29
客户端:
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();
}
} }
}
最新文章
- php5.6+apache2.4环境配置
- 用Python向MySQL数据库插入数据
- Dynamics AX 2012 R2 为运行失败的批处理任务设置预警
- memset的使用
- 性能优化-查询最耗CPU的SESSION与SQL
- 函数rec_get_nth_field_offs_old
- IP地址变动后,https://localhost:1158/em无法访问解决办法
- Eclipse 完全 设置UTF-8 环境
- rsyslog ~ 波浪号
- MongoDB学习(翻译4)
- 关于云Linux部署tomcat服务器(Maven的多模块war包)
- java中String和StringBuffer的区别
- 文件操作 day8
- SVN Hooks的介绍及使用
- liferay笑傲江湖-API之参数的工具类(ParamUtil)
- Source引擎多人模式网络同步模型
- nodejs 接收上传的图片
- JS实现幸运抽奖页面
- 8.初识Lock与AbstractQueuedSynchronizer(AQS)
- LeetCode 621. Task Scheduler
热门文章
- HTML5学习笔记 Web存储
- Linux下源码安装JDK7
- c# 高效的线程安全队列ConcurrentQueue
- selenium 实现浏览器 前进和后退
- action(一)
- 聊一聊HTML <;pre>;标签
- dependent-name ‘xxx::yyy’ is parsed as a non-type, but instantiation yields a type
- PHP之文件大小的转换函数
- Android——RelativeLayout(相对布局)
- josn 格式 解析