java

位置:IT落伍者 >> java >> 浏览文章

Java socket应用的几个小实例


发布日期:2022年01月14日
 
Java socket应用的几个小实例

用于本机上的客户端与服务器对话的小程序

客户端

import javaioBufferedReader;

import javaioBufferedWriter;

import javaioInputStreamReader;

import javaioOutputStreamWriter;

import Socket;

public class Client extends Thread

{

public void Write() throws Exception

{

Socket s=new Socket();

BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(sgetOutputStream()));

BufferedReader br=new BufferedReader(new InputStreamReader(Systemin));

String message=客户端说:;

while(!(messageequals()))

{

message=brreadLine();

Systemoutprintln(客户端说:+message);

bwwrite(message+\n);

bwflush();

}

bwclose();

sclose();

}

public void run()

{

Socket m=null;

BufferedReader dr=null;

try

{

m=new Socket();

dr=new BufferedReader(new InputStreamReader(mgetInputStream()));

String msg=服务器说:;

while(!(msgequals()))

{

msg=drreadLine();

Systemoutprintln(服务器说:+msg);

}

}

catch(Exception e){eprintStackTrace();}

finally

{

try

{

drclose();

mclose();

}

catch(Exception e){eprintStackTrace();}

}

}

public static void main(String args[])throws Exception

{

Client c=new Client();

cstart();

cWrite();

}

}

服务器

import javaioBufferedReader;

import javaioBufferedWriter;

import javaioInputStreamReader;

import javaioOutputStreamWriter;

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=ssaccept();

BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(sgetOutputStream()));

BufferedReader br=new BufferedReader(new InputStreamReader(Systemin));

String message=服务器说:;

while(!(messageequals()))

{

message=brreadLine();

Systemoutprintln(服务器说:+message);

bwwrite(message+\n);

bwflush();

}

//bwclose();

sclose();

}

public void run()

{

Socket m=null;

BufferedReader dr=null;

try

{

m=ssaccept();

dr=new BufferedReader(new InputStreamReader(mgetInputStream()));

String msg=客户端说:;

while(!(msgequals()))

{

msg=drreadLine();

Systemoutprintln(客户端:+msg);

}

}

catch(Exception e){eprintStackTrace();}

finally

{

try

{

drclose();

mclose();

}

catch(Exception e){eprintStackTrace();}

}

}

public static void main(String args[])throws Exception

{

Server s=new Server();

sstart();

sWrite();

}

}

用于本地群聊的小程序

客户端

import javaioBufferedReader;

import javaioBufferedWriter;

import javaioInputStreamReader;

import javaioOutputStreamWriter;

import Socket;

public class Friendclient extends Thread

{

public void Writer() throws Exception

{

Socket s=new Socket();

BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(sgetOutputStream()));

BufferedReader br=new BufferedReader(new InputStreamReader(Systemin));

String message=本客户端说:;

while(!(messageequals(goodbye)))

{

message=brreadLine();

Systemoutprintln(客户端说:+message);

bwwrite(message+\n);

bwflush();

}

bwclose();sclose();

}

public void run()

{

BufferedReader bq=null;

Socket st=null;

try

{

st=new Socket();

bq=new BufferedReader(new InputStreamReader(stgetInputStream()));

String msg=其他客户端说:;

while(!(msgequals()))

{

msg=bqreadLine();

Systemoutprintln(其他客户端说:+msg);

}

}

catch(Exception e){eprintStackTrace();}

finally

{

try

{

bqclose();stclose();

}

catch(Exception e){eprintStackTrace();}

}

}

public static void main(String args[])throws Exception

{

Friendclient ft=new Friendclient();

ftstart();

ftWriter();

}

}

服务器端

import javaioBufferedReader;

import javaioBufferedWriter;

import javaioInputStreamReader;

import javaioOutputStreamWriter;

import ServerSocket;

import Socket;

import javautilArrayList;

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=ssaccept();

aladd(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

{

thissd=sd;

br=new BufferedReader(new InputStreamReader(sdgetInputStream()));

//bw=new BufferedWriter(new OutputStreamWriter(sdgetOutputStream()));

}

public void run()

{

try

{

String gh=;

while(true)

{

gh=brreadLine();

setAll(gh);

}

}

catch(Exception e){eprintStackTrace();}

}

public void setAll(String x)throws Exception

{

Socket ss=null;

BufferedWriter bv=null;

for(int i=;i<alsize();i++)

{

ss=(Socket)alget(i);

bv=new BufferedWriter(new OutputStreamWriter(ssgetOutputStream()));

bvwrite(x+\n);

bvflush();

}

}

}

public static void main(String args[])throws Exception

{

new Allserver();

}

}

               

上一篇:JAVA中各类CACHE机制实现的比较

下一篇:JAVA版StarDict星际译王简单实现