科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道Java socket 入门编程实例

Java socket 入门编程实例

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

Java socket 入门编程实例

作者:472956429 来源:赛迪网技术社区 2007年11月30日

关键字: 编程 socket

  • 评论
  • 分享微博
  • 分享邮件
http://tech.163.com  =?pW 3  
xIh|7+\  
这是一个C/S之间通信的例子,在JDK1.4下测试通过. ? YA88<  
//服务器端源程序tcpserver.java PWwdn0{  
nzVAnn84W  
import java.io.*; G4Me!w9{z  
import java.net.*; D!W:  
public class tcpserver , {%I/Ajly  
{ x?RFX#Y  
public static void main(String[] args) throws IOException U.Qy(ld  
{ :iff4 !K!  
ServerSocket svrsoc=null; Y13#^y!}  
Socket soc=null; ZolLSF  
DataInputStream in=null; r-Wvi%  
PrintStream out=null; mtMDb 9Vb  
InetAddress clientIP=null; hrFX)13/7  
String str=null; }~Fs%$5  
try e{ M+Qzk  
{ c]TqQr~Xx  
svrsoc=new ServerSocket(8000); 8 SLY~  
System.out.println("Server start...."); mQ|u a 'D  
soc=svrsoc.accept(); iN.0rDE}&  
/CUrO{.E7  
in=new DataInputStream(soc.getInputStream()); _R9= .k  
out=new PrintStream(soc.getOutputStream()); [+ Hm:--  
clientIP=soc.getInetAddress(); { A=ez  
System.out.println("Client's IP address:"+clientIP); `wIk*l;  
out.println("welcome....."); O*F']G=  
str=in.readLine(); +}=X.>M^  
while (!str.equals("quit")) I Z t?5{Q  
{ C-5> `r  
System.out.println("Client said:"+str); -aF *8X  
str=in.readLine(); %V{e0D  
} & P3.A  
System.out.println("Client want to leave"); 7XNkTjC  
} sr*(C&+z  
catch(Exception e) 2X{|kl1WI  
{ )ZgXfB  
System.out.println("error:"+e); {@iN,d  
} bBt/4ncGB  
finally V>A67vg  
{ .Z29Dc=A  
in.close(); -5f"R}"`  
out.close(); +izkV_  
soc.close(); {w` 7=]  
svrsoc.close(); J[862G  
System.exit(0); [N55b2y\  
} w]U}~qq  
} 3GbQ-h?b  
} l65#*   
Nng nM\N  
//客户端源程序tcpclient.java ;Nr/ "  
#ye UE?  
import java.io.*; 'NNKyG]D  
import java.net.*; x+/]Ns-B  
public class tcpclient  Nza`cDs  
{ x v/gT/$  
public static void main(String[] args) throws IOException P6!]lm)  
{ lj2Pvvsx  
Socket soc=null; ^\*WT}:  
DataInputStream in=null; 5?:@[UQ  
PrintStream out=null; |`lFmgX  
DataInputStream sysin=null; ,kf`VMiB  
String strin=null; &42(I<+-(  
String strout=null; AUx9(yA l  
try v+LdI>F7  
{ mT@0/ 8c   
soc=new Socket(args[0],8000); (dA%*^o`\  
System.out.println("Connecting to the Server"); , i")7Rn[  
in=new DataInputStream(soc.getInputStream()); =jJ =J(of^  
out=new PrintStream(soc.getOutputStream()); p6e9<L.?|  
strin=in.readLine(); O{h6l|#  
System.out.println("Server said:"+strin); l@r]+=X6-  
sysin=new DataInputStream(System.in); PXS( >l  
strout=sysin.readLine(); + J{Efi  
while (!strout.equals("quit")) v[#RMB*w:3  
{ Fj*_Be;  
out.println(strout); l5VnXF_  
strout=sysin.readLine(); Z!K=qa"  
} &=GKC94>H  
out.println(strout); fL]J}/oo6  
} yh1rI*hb  
catch(Exception e) gvVBYfC 3  
{ GLeSuYq  
System.out.println("error:"+e); E-pI5C  
} hCf& nDi  
finally g=k8)d  
{ GV#kYfy>  
in.close(); X%!FP%  
out.close(); q X|[y  
soc.close(); `_Z4iFB  
sysin.close();  Q ;&8K  
System.exit(0); C>lB+#oU  
} [8As8a)   
} S1'A4/Z  
}
查看本文来源
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章