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

轻云互联,香港云服务器折后22元/月 美国云服务器 1核 512M内存 15M带宽 折后19.36元/月

轻云互联成立于2018年的国人商家,广州轻云互联网络科技有限公司旗下品牌,主要从事VPS、虚拟主机等云计算产品业务,适合建站、新手上车的值得选择,香港三网直连(电信CN2GIA联通移动CN2直连);美国圣何塞(回程三网CN2GIA)线路,所有产品均采用KVM虚拟技术架构,高效售后保障,稳定多年,高性能可用,网络优质,为您的业务保驾护航。官方网站:点击进入广州轻云网络科技有限公司活动规则:1.用户购...

hostio荷兰10Gbps带宽,10Gbps带宽,€5/月,最低配2G内存+2核+5T流量

成立于2006年的荷兰Access2.IT Group B.V.(可查:VAT: NL853006404B01,CoC: 58365400) 一直运作着主机周边的业务,当前正在对荷兰的高性能AMD平台的VPS进行5折优惠,所有VPS直接砍一半。自有AS208258,vps母鸡配置为Supermicro 1024US-TRT 1U,2*AMD Epyc 7452(64核128线程),16条32G D...

HaBangNet(6.95美元/月)美国vps 5TB流量/德国vps 香港双向CN2 GIA VPS

HaBangNet支持支付宝和微信支付,只是价格偏贵,之前国内用户并不多。这次HaBangNet推出三个特价套餐,其中美国机房和德国机房价格也还可以,但是香港机房虽然是双向CN2 GIA线路,但是还是贵的惊人,需要美国和德国机房的可以参考下。HaBangNet是一家成立于2014年的香港IDC商家,中文译名:哈邦网络公司,主营中国香港、新加坡、澳大利亚、荷兰、美国、德国机房的虚拟主机、vps、专用...

javasocket为你推荐
谷歌德语在线翻译德语翻译 不找你不代表我不想你 别用翻译器 谢谢~qq空间维护qq空间打不开怎么维护goalgoalgoal哆啦小子和哆啦A梦是一个型号的吗?微软将停止支持32位Win10系统WIN10系统Microsoft Office已停止工作怎么解决无法清除dns缓存Windows无法清除DNS缓存罗振宇2017跨年演讲“时间的朋友”跨年演讲办了多少场,分别是什么主题?pps电影PPS上最近有什么电影值得观看pps电影PPS里面好看的电影.推荐一下.大数据的分类什么是大数据,它有哪些特点移动上网设置手机上网参数设置方法
新网域名 Oray域名注册服务商 上海服务器租用 汉邦高科域名申请 阿里云os 新世界机房 softlayer 南昌服务器托管 qingyun 空间出租 网站卫士 智能dns解析 登陆qq空间 博客域名 聚惠网 ncp是什么 时间服务器 apache启动失败 phpwind论坛 weblogic部署 更多