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

昔日数据月付12元起,湖北十堰机房10M带宽月付19元起

昔日数据怎么样?昔日数据是一个来自国内服务器销售商,成立于2020年底,主要销售国内海外云服务器,目前有国内湖北十堰云服务器和香港hkbn云服务器 采用KVM虚拟化技术构架,湖北十堰机房10M带宽月付19元起;香港HKBN,月付12元起; 此次夏日活动全部首月5折促销,有需要的可以关注一下。点击进入:昔日数据官方网站地址昔日数据优惠码:优惠码: XR2021 全场通用(活动持续半个月 2021/7...

VinaHost,越南vps,国内延时100MS;不限流量100Mbps

vinahost怎么样?vinahost是一家越南的主机商家,至今已经成13年了,企业运营,老牌商家,销售VPS、虚拟主机、域名、邮箱、独立服务器等,机房全部在越南,有Viettle和VNPT两个机房,其中VNPT机房中三网直连国内的机房,他家的产品优势就是100Mbps不限流量。目前,VinaHost商家发布了新的优惠,购买虚拟主机、邮箱、云服务器、VPS超过三个月都有赠送相应的时长,最高送半年...

VoLLcloud(月付低至2.8刀)香港vps大带宽,三网直连

VoLLcloud LLC是一家成立于2020年12月互联网服务提供商企业,于2021年1月份投入云计算应用服务,为广大用户群体提供云服务平台,已经多个数据中心部署云计算中心,其中包括亚洲、美国、欧洲等地区,拥有自己的研发和技术服务团队。现七夕将至,VoLLcloud LLC 推出亚洲地区(香港)所有产品7折优惠,该产品为CMI线路,去程三网163,回程三网CMI线路,默认赠送 2G DDoS/C...

javasocket为你推荐
stm32视频教程STM32 新手要怎么才能快捷的入门,有那种一边写代码一边视频教学的视频教程吗?安卓性能测试工具三款安卓系统手机测试软件哪个好用iphone12或支持北斗导航iphone12是5G手机吗?iphone12或支持北斗导航苹果12屏幕设置magento模板magento1.9主题开发用哪个模板为基础好?电梯物联网平台国内物联网公司排名,知道的帅哥美女快来回答哦!谢谢了!智能公共广播系统有谁知道公共广播系统都包括那些设备做视频的免费软件有没有免费做视频的软件 汉化的handoff怎么用如何令Yosemite使用iPhone的通话功能和Handoff设置联通玩电信游戏关于联通到底能不能去电信区玩游戏。。。。
提供香港vps 高防直连vps dreamhost 香港机房托管 wdcp 360抢票助手 godaddy域名转出 info域名 阿里云代金券 12306抢票助手 hostloc 香港新世界中心 香港亚马逊 linode支付宝 美国迈阿密 1美元 网站防护 数据湾 开心online linux服务器系统 更多