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

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

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

ftlcloud9元/月,美国云服务器,1G内存/1核/20g硬盘/10M带宽不限/10G防御

ftlcloud(超云)目前正在搞暑假促销,美国圣何塞数据中心的云服务器低至9元/月,系统盘与数据盘分离,支持Windows和Linux,免费防御CC攻击,自带10Gbps的DDoS防御。FTL-超云服务器的主要特色:稳定、安全、弹性、高性能的云端计算服务,快速部署,并且可根据业务需要扩展计算能力,按需付费,节约成本,提高资源的有效利用率。活动地址:https://www.ftlcloud.com...

gcorelabs:CDN业务节点分布100多个国家地区,免费版提供1T/月流量

卢森堡商家gcorelabs是个全球数据中心集大成的运营者,不但提供超过32个数据中心的VPS、13个数据中心的cloud(云服务器)、超过44个数据中心的独立服务器,还提供超过100个数据中心节点的CDN业务。CDN的总带宽容量超过50Tbps,支持免费测试! Gcorelabs根据业务分,有2套后台,分别是: CDN、流媒体平台、DDoS高防业务、块存储、cloud云服务器、裸金属服务器...

javasocket为你推荐
安卓系统软件删除安卓系统中如何删除无用程序?iphone12或支持北斗导航iphone12是问题机吗印象城市游戏论坛游聚游戏平台如何使用?创业好项目论坛大学生创业有什么好的项目啊?flv转换aviflv怎么转换成aviangel的意思Angel、的中文意思魔兽世界密保卡魔兽世界的密保卡怎么申请qq空间克隆素材qq空间克隆微软永久关闭实体店如何注销微软账户发送验证码关联手机号码发送短信验证码
下载虚拟主机 t楼 vps.net rak机房 租空间 129邮箱 php空间购买 天翼云盘 江苏双线服务器 厦门电信 cloudlink 百度云加速 中国联通宽带测试 存储服务器 移动王卡 什么是dns 时间同步服务器 网络安装 rewrite规则 qq部落24-5 更多