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(); } } } }

印象云七夕促销,所有机器7折销售,美国CERA低至18元/月 年付217元!

印象云,成立于2019年3月的商家,公司注册于中国香港,国人运行。目前主要从事美国CERA机房高防VPS以及香港三网CN2直连VPS和美国洛杉矶GIA三网线路服务器销售。印象云香港三网CN2机房,主要是CN2直连大陆,超低延迟!对于美国CERA机房应该不陌生,主要是做高防服务器产品的,并且此机房对中国大陆支持比较友好,印象云美国高防VPS服务器去程是163直连、三网回程CN2优化,单IP默认给20...

RAKsmart推出7.59美元/月,云服务器产品Cloud Server,KVM架构1核1G内存40G硬盘1M带宽基础配置

近期RAKsmart上线云服务器Cloud Server产品,KVM架构1核1G内存40G硬盘1M带宽基础配置7.59美元/月!RAKsmart云服务器Cloud Server位于美国硅谷机房,下单可选DIY各项配置,VPC网络/经典网络,大陆优化/精品网线路,1-1000Mbps带宽,支持Linux或者Windows操作系统,提供Snap和Backup。RAKsmart机房是一家成立于2012年...

数脉科技:六月优惠促销,免备案香港物理服务器,E3-1230v2处理器16G内存,350元/月

数脉科技六月优惠促销发布了!数脉科技对香港自营机房的香港服务器进行超低价促销,可选择30M、50M、100Mbps的优质bgp网络。更大带宽可在选购时选择同样享受优惠,目前仅提供HKBGP、阿里云产品,香港CN2、产品优惠码续费有效,仅限新购,每个客户可使用于一个订单。新客户可以立减400元,或者选择对应的机器用相应的优惠码,有需要的朋友可以尝试一下。点击进入:数脉科技官方网站地址数脉科技是一家成...

javasocket为你推荐
nasa中文官网NASA总部在哪里?windowsphone手机Windows phone手机的优点和缺点dnf客户端消失地下城勇士登录 客户端消失云图好看吗云图这部电影好看吗云图好看吗云图好看么?云图好看吗电影云图好看吗?cf服务器爆满为什么穿越火线显示该服务器已爆满怎么才能挤进去?handoff怎么用Mac的Hand Off怎么连接iPhone?prepare的用法prepare和prepare for的区别双色球在线选号器双色球 选号
过期备案域名查询 美国vps 高防dns t楼 流媒体服务器 12u机柜尺寸 debian7 服务器架设 国内php空间 河南服务器 太原联通测速平台 linux空间 数字域名 七夕快乐英文 搜索引擎提交入口 ca187 银盘服务 yundun 东莞idc 河南移动梦网 更多