用於本機上的客戶端與服務器對話的小程序
客戶端
import java
import java
import java
import java
import
public class Client extends Thread
{
public void Write() throws Exception
{
Socket s=new Socket(
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s
BufferedReader br=new BufferedReader(new InputStreamReader(System
String message=
while(!(message
{
message=br
System
bw
bw
}
bw
s
}
public void run()
{
Socket m=null;
BufferedReader dr=null;
try
{
m=new Socket(
dr=new BufferedReader(new InputStreamReader(m
String msg=
while(!(msg
{
msg=dr
System
}
}
catch(Exception e){e
finally
{
try
{
dr
m
}
catch(Exception e){e
}
}
public static void main(String args[])throws Exception
{
Client c=new Client();
c
c
}
}
服務器
import java
import java
import java
import java
import
import
public class Server extends Thread
{
private ServerSocket ss=null;
public Server ()throws Exception
{
ss=new ServerSocket(
}
public void Write()throws Exception
{
Socket s=ss
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s
BufferedReader br=new BufferedReader(new InputStreamReader(System
String message=
while(!(message
{
message=br
System
bw
bw
}
//bw
s
}
public void run()
{
Socket m=null;
BufferedReader dr=null;
try
{
m=ss
dr=new BufferedReader(new InputStreamReader(m
String msg=
while(!(msg
{
msg=dr
System
}
}
catch(Exception e){e
finally
{
try
{
dr
m
}
catch(Exception e){e
}
}
public static void main(String args[])throws Exception
{
Server s=new Server();
s
s
}
}
用於本地群聊的小程序
客戶端
import java
import java
import java
import java
import
public class Friendclient extends Thread
{
public void Writer() throws Exception
{
Socket s=new Socket(
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s
BufferedReader br=new BufferedReader(new InputStreamReader(System
String message=
while(!(message
{
message=br
System
bw
bw
}
bw
}
public void run()
{
BufferedReader bq=null;
Socket st=null;
try
{
st=new Socket(
bq=new BufferedReader(new InputStreamReader(st
String msg=
while(!(msg
{
msg=bq
System
}
}
catch(Exception e){e
finally
{
try
{
bq
}
catch(Exception e){e
}
}
public static void main(String args[])throws Exception
{
Friendclient ft=new Friendclient();
ft
ft
}
}
服務器端
import java
import java
import java
import java
import
import
import java
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
al
new Mtherd(s)
}
}
private class Mtherd extends Thread
{
private Socket sd=null;
private BufferedReader br=null;
//private BufferedWriter bw=null;
public Mtherd(Socket sd)throws Exception
{
this
br=new BufferedReader(new InputStreamReader(sd
//bw=new BufferedWriter(new OutputStreamWriter(sd
}
public void run()
{
try
{
String gh=
while(true)
{
gh=br
setAll(gh);
}
}
catch(Exception e){e
}
public void setAll(String x)throws Exception
{
Socket ss=null;
BufferedWriter bv=null;
for(int i=
{
ss=(Socket)al
bv=new BufferedWriter(new OutputStreamWriter(ss
bv
bv
}
}
}
public static void main(String args[])throws Exception
{
new Allserver();
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25528.html