用java進行網絡編程確實很方便
首先
//
ServerSocket ss = new ServerSocket(PortNumber);
Socket s = ss
//
OutputStream os = s
BufferedOutputStream bos = new BufferedOutputStream(os);
InputStream is = s
//
// 服務器
//os
bos
bos
// 服務器 <
byte[] buf = new byte[
int len = is
System
//
//os
bos
is
s
ss
作為客戶端則分為下面幾個步驟
//
Socket s = new Socket(InetAddress
PortNumber);
//
OutputStream os = s
InputStream is = s
//
// 服務器
byte[] buf = new byte[
int len = is
System
// 服務器 <
os
//
os
is
s
上面這些只是一個單線程的服務端
這時
代碼如下
//Server
import java
import
import java
public class Server extends Thread {
ServerSocket skt;
Socket Client[]=new Socket[
Socket Client
int i =
TextArea in;
int port
//BufferedReader theInputStream;
PrintStream theOutputStream;
//String readin;
Face chat;
public Server(int port
try {
this
skt = new ServerSocket(port);
this
} catch (IOException e) {
chat
}
}
public void run() {
chat
while (true) {
//System
try {
Client[k] = skt
//當有客戶端連接時就新建一個子線程
if (i <
ServerThread server[] = new ServerThread[
server[k]= new ServerThread(Client[k]
l=server
server[k]
chat
/*theInputStream = new BufferedReader(new InputStreamReader(Client
//for(int j=
theOutputStream = new PrintStream(server[k]
i = server[k]
k++;
} else {
//theOutputStream = new PrintStream(null);
}
} catch (SocketException e) {
//chat
//chat
//chat
//chat
//chat
//try {
//skt
//Client
//} catch (IOException err) {
// chat
//}
} catch (IOException e) {
chat
}
}
}
public void dataout(String data) {
//for(int j=
theOutputStream
}
}
class ServerThread extends Thread {
ServerSocket skt;
Socket Client;
TextArea in;
int port;
int i;
BufferedReader theInputStream;
PrintStream theOutputStream;
String readin;
Face chat;
//服務端子線程
public ServerThread(Socket s
this
Client = s;
//this
//skt = new ServerSocket(port);
this
}
public int getI() {
return this
}
public Socket getClient() {
return this
}
public void run() {
//chat
try {
//Client = skt
//chat
theInputStream = new BufferedReader(new InputStreamReader(Client
theOutputStream = new PrintStream(Client
while (true) {
readin = theInputStream
chat
}
} catch (SocketException e) {
chat
chat
chat
chat
chat
try {
i
skt
Client
} catch (IOException err) {
chat
}
} catch (IOException e) {
chat
}
}
public void dataout(String data) {
theOutputStream
}
}
//Client
import
import java
import javax
class Client extends Thread {
Socket skt;
InetAddress host;
int port;
BufferedReader theInputStream;
PrintStream theOutputStream;
String readin;
Face chat;
public Client(String ip
try {
host = InetAddress
port = p;
this
} catch (IOException e) {
chat
}
}
public void run() {
try {
chat
skt = new Socket(host
chat
theInputStream = new BufferedReader(new InputStreamReader(skt
theOutputStream = new PrintStream(skt
//Timer myTimer = new Timer();
while (true) {
readin = theInputStream
chat
}
} catch (SocketException e) {
chat
chat
chat
chat
chat
try {
skt
} catch (IOException err) {
chat
}
} catch (IOException e) {
chat
}
}
public void dataout(String data) {
theOutputStream
}
}
//軟件界面
//face
import java
import java
public class Face extends Frame {
/**
*
*/
private static final long serialVersionUID =
Button clientBtn
TextArea ta;
TextField tfaddress
int port;
Client client;
Server server;
boolean iamserver;
static Face frm;
public Face() {
clientBtn = new Button(
serverBtn = new Button(
ta = new TextArea(
tfaddress = new TextField(
tfport = new TextField(
tftype = new TextField(
tftype
ta
setLayout(new FlowLayout());
add(tfaddress);
add(tfport);
add(clientBtn);
add(serverBtn);
add(ta);
add(tftype);
setSize(
setTitle(
this
clientBtn
public void actionPerformed(ActionEvent e) {
port = Integer
client = new Client(tfaddress
client
tfaddress
tfport
serverBtn
clientBtn
}
});
serverBtn
public void actionPerformed(ActionEvent e) {
port = Integer
server = new Server(port
server
iamserver = true;
tfaddress
tfaddress
tfport
serverBtn
clientBtn
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System
}
});
}
public static void main(String args[]) {
frm = new Face();
}
private class TFListener implements KeyListener {
public void keyPressed(KeyEvent e) {
if (e
ta
if (iamserver)
server
else
client
tftype
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
}
這個程序我限制了連接數
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26868.html