javasocket用java socket 写一个简单的 聊天 客户端与服务器端之间 都能实现 一个不

javasocket  时间:2021-07-20  阅读:()

想学java 的socket ,怎么学?

lz只是为了用java的socket啊,那么我给你这么介绍下,你学了java的基础语法后,你得掌握下java的多线程,因为 java的socket几乎是和多线程一起使用的,对于socket还要学习的就是流了这是socket传输内容的东西。

如果lz学会了这两个东西,那么恭喜你,你可以学习socket的api了,学习socket的api在网上的文档还是有的,但是你得分辨哪些是好的,哪些是转载的,对于socket的学习建议lz不要看什么视频了因为网上的视频都太简单了,就类似于刚想java的时候的hello world程序级别的视频。

最后祝lz早日学成正果

java socket编程

private void connectServer(String ip,int port){//连接 try{ if(jButton1.getText().trim().equals("连接")){ jButton1.setText("连接服务器..."); socket=new Socket(ip,port); MyThread t=new MyThread(); t.start(); } }catch(Exception ex){ System.exit(0); } }

用java socket 写一个简单的 聊天 客户端与服务器端之间 都能实现 一个不

客户端代码: ; import .*; import java.io.*; public class socketclient { public static void main(String[] args) { Socket s = null; InputStream is = null; DataInputStream d = null; OutputStream os = null; DataOutputStream ds = null; try { s = new Socket("127.0.0.1", 1555); is = s.getInputStream(); d = new DataInputStream(is); os = s.getOutputStream(); ds = new DataOutputStream(os); System.out.println(d.readUTF() + " from " + s.getPort()); String content = null; while (true) { content = new BufferedReader(new InputStreamReader(System.in)) .readLine(); ds.writeUTF("port# " + s.getLocalPort() + " says " + content + " to " + s.getPort()); ds.flush(); if (content.equalsIgnoreCase("Bye")) break; } } catch (ConnectException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { ds.close(); os.close(); d.close(); is.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } } } 服务器端代码: ; import .*; import java.io.*; public class socketserver { public static void main(String[] args) { Socket s = null; OutputStream out = null; DataOutputStream ds = null; InputStream is = null; DataInputStream d = null; try { ServerSocket ss = new ServerSocket(1555); s = ept(); out = s.getOutputStream(); ds = new DataOutputStream(out); is = s.getInputStream(); d = new DataInputStream(is); } catch (IOException e) { e.printStackTrace(); } try { String content = null; ds.writeUTF("Hello " + Address() + " port#" + s.getPort()); ds.flush(); while (true) { content = d.readUTF(); System.out.println(content); if (content.equalsIgnoreCase("Bye")) break; } } catch (IOException e) { e.printStackTrace(); } finally { try { ds.close(); out.close(); d.close(); is.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } } }

UCloud云服务器低至年59元

最近我们是不是在讨论较多的是关于K12教育的问题,培训机构由于资本的介入确实让家长更为焦虑,对于这样的整改我们还是很支持的。实际上,在云服务器市场中,我们也看到内卷和资本的力量,各大云服务商竞争也是相当激烈,更不用说个人和小公司服务商日子确实不好过。今天有看到UCloud发布的夏季促销活动,直接提前和双十一保价挂钩。这就是说,人家直接在暑假的时候就上线双十一的活动。早年的双十一活动会提前一周到十天...

白丝云-美国圣何塞4837/德国4837大带宽/美西9929,26元/月起

官方网站:点击访问白丝云官网活动方案:一、KVM虚拟化套餐A1核心 512MB内存 10G SSD硬盘 800G流量 2560Mbps带宽159.99一年 26一月套餐B1核心 512MB内存 10G SSD硬盘 2000G流量 2560Mbps带宽299.99一年 52一月套餐...

CUBECLOUD:香港服务器、洛杉矶服务器、全场88折,69元/月

CUBECLOUD(魔方云)成立于2016年,亚太互联网络信息中心(APNIC)会员,全线产品均为完全自营,专业数据灾备冗余,全部产品均为SSD阵列,精品网络CN2(GIA) CU(10099VIP)接入,与当今主流云计算解决方案保持同步,为企业以及开发者用户实现灵活弹性自动化的基础设施。【夏日特促】全场产品88折优惠码:Summer_2021时间:2021年8月1日 — 2021年8月8日香港C...

javasocket为你推荐
im社区什么是IWOM?im社区什么是Openfire,XMPP,这类人才集中在哪些领域美国大选投票实时数据美国大选每个州的选举人票多少是怎么定的微软将停止支持32位Win10系统微软即将停止支持当前操作系统,如果不升级会怎么样windows7系统要求windows7的系统盘要求最低是多少2020带来好运的微信头像女人带来好运的微信网名是什么?vs2005快捷键vs中的一个快捷键linux操作系统好吗linux好用不?罗振宇2017跨年演讲第五届“时间的朋友”跨年演讲关键词:基本盘,什么叫“基本盘”?pps电影PPS里面好看的电影.推荐一下.
免费vps vps虚拟服务器 个人域名备案 ipage 美国主机评论 国外php主机 密码泄露 智能骨干网 河南移动邮件系统 vip购优惠 美国免费空间 福建铁通 metalink 台湾谷歌 360云服务 腾讯总部在哪 阿里云官方网站 电信网络测速器 supercache 广东服务器托管 更多