知识大全 Java socket应用的几个小实例
Posted 知
篇首语:幽映每白日,清辉照衣裳。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Java socket应用的几个小实例相关的知识,希望对你有一定的参考价值。
Java socket应用的几个小实例 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
用于本机上的客户端与服务器对话的小程序
客户端
import java io BufferedReader;
import java io BufferedWriter;
import java io InputStreamReader;
import java io OutputStreamWriter;
import Socket;
public class Client extends Thread
public void Write() throws Exception
Socket s=new Socket( );
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s getOutputStream()));
BufferedReader br=new BufferedReader(new InputStreamReader(System in));
String message= 客户端说: ;
while(!(message equals( )))
message=br readLine();
System out println( 客户端说: +message);
bw write(message+ \\n );
bw flush();
bw close();
s close();
public void run()
Socket m=null;
BufferedReader dr=null;
try
m=new Socket( );
dr=new BufferedReader(new InputStreamReader(m getInputStream()));
String msg= 服务器说: ;
while(!(msg equals( )))
msg=dr readLine();
System out println( 服务器说: +msg);
catch(Exception e)e printStackTrace();
finally
try
dr close();
m close();
catch(Exception e)e printStackTrace();
public static void main(String args[])throws Exception
Client c=new Client();
c start();
c Write();
服务器
import java io BufferedReader;
import java io BufferedWriter;
import java io InputStreamReader;
import java io OutputStreamWriter;
import ServerSocket;
import Socket;
public class Server extends Thread
private ServerSocket ss=null;
public Server ()throws Exception
ss=new ServerSocket( );
public void Write()throws Exception
Socket s=ss accept();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s getOutputStream()));
BufferedReader br=new BufferedReader(new InputStreamReader(System in));
String message= 服务器说: ;
while(!(message equals( )))
message=br readLine();
System out println( 服务器说: +message);
bw write(message+ \\n );
bw flush();
//bw close();
s close();
public void run()
Socket m=null;
BufferedReader dr=null;
try
m=ss accept();
dr=new BufferedReader(new InputStreamReader(m getInputStream()));
String msg= 客户端说: ;
while(!(msg equals( )))
msg=dr readLine();
System out println( 客户端: +msg);
catch(Exception e)e printStackTrace();
finally
try
dr close();
m close();
catch(Exception e)e printStackTrace();
public static void main(String args[])throws Exception
Server s=new Server();
s start();
s Write();
用于本地群聊的小程序
客户端
import java io BufferedReader;
import java io BufferedWriter;
import java io InputStreamReader;
import java io OutputStreamWriter;
import Socket;
public class Friendclient extends Thread
public void Writer() throws Exception
Socket s=new Socket( );
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s getOutputStream()));
BufferedReader br=new BufferedReader(new InputStreamReader(System in));
String message= 本客户端说: ;
while(!(message equals( goodbye )))
message=br readLine();
System out println( 客户端说: +message);
bw write(message+ \\n );
bw flush();
bw close();s close();
public void run()
BufferedReader bq=null;
Socket st=null;
try
st=new Socket( );
bq=new BufferedReader(new InputStreamReader(st getInputStream()));
String msg= 其他客户端说: ;
while(!(msg equals( )))
msg=bq readLine();
System out println( 其他客户端说: +msg);
catch(Exception e)e printStackTrace();
finally
try
bq close();st close();
catch(Exception e)e printStackTrace();
public static void main(String args[])throws Exception
Friendclient ft=new Friendclient();
ft start();
ft Writer();
服务器端
import java io BufferedReader;
import java io BufferedWriter;
import java io InputStreamReader;
import java io OutputStreamWriter;
import ServerSocket;
import Socket;
import java util ArrayList;
public class Allserver
private ServerSocket ss=null;
private static ArrayList<Socket> al=new ArrayList<Socket>();
public Allserver() throws Exception
ss=new ServerSocket( );
while(true)
Socket s=ss accept();
al add(s);
new Mtherd(s) start();
private class Mtherd extends Thread
private Socket sd=null;
private BufferedReader br=null;
//private BufferedWriter bw=null;
public Mtherd(Socket sd)throws Exception
this sd=sd;
br=new BufferedReader(new InputStreamReader(sd getInputStream()));
//bw=new BufferedWriter(new OutputStreamWriter(sd getOutputStream()));
public void run()
try
String gh= ;
while(true)
gh=br readLine();
setAll(gh);
catch(Exception e)e printStackTrace();
public void setAll(String x)throws Exception
Socket ss=null;
BufferedWriter bv=null;
for(int i= ;i<al size();i++)
ss=(Socket)al get(i);
bv=new BufferedWriter(new OutputStreamWriter(ss getOutputStream()));
bv write(x+ \\n );
bv flush();
public static void main(String args[])throws Exception
new Allserver();
cha138/Article/program/Java/hx/201311/25528
相关参考
要把一个厨房装修的有品位且实用,呈现最佳效果,细节是务必追求的环节。装饰网专家在这里传授18个厨房装修的秘笈,让你的厨房更美,更实用,更持久。煤气管道到厨房煤气热水器之间不要走明管,在墙上开个槽埋PV
实例教程:JavaSocket编程的一个秘密类[2] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
实例教程:JavaSocket编程的一个秘密类[1] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
卫生间装修马桶选购有几个小技巧?于座便器的几个要点,马桶是卫生间中三大件之一,是卫生间装修的重要主材。要想选择一款适合自己的马桶,首先要对马桶有个初步的了解,作为日常生活的必须品,一个舒适美观、质量上
Java数据库编程中的几个常用技巧 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! java数据库
Java编程语言的几个认识误区 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!cha138
一个将SQL语句嵌入Java应用程序的实例 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!我们在将S
Java开源大全 Java爱好者 Java学习室 Java资源
ServerSocket通信简单的实例分析 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Ser
将Java应用程序本地编译为EXE的几种方法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 从获