能够同时处理多个客户的服务器
2008-10-18 15:52
204 查看
客户端
package cs;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class Cient {
/**
* @Weslysong
*/
public static void main(String[] args) {
try {
Socket connection = new Socket(args[0], 2545);
System.out.println("建立连接");
DataInputStream dis = new DataInputStream(connection
.getInputStream());
DataOutputStream dos = new DataOutputStream(connection
.getOutputStream());
String line = new String("");
while (!line.toUpperCase().equals("QUIT")) {
System.out.println("enter String:");
line = readString();// 类方法
System.out.println("发送到服务器");
dos.writeUTF(line);
System.out.println("等待服务器相应");
line = dis.readUTF();
System.out.println("服务器:" + line);
}
dis.close();
dos.close();
connection.close();
} catch (UnknownHostException e) { System.out.println("unknow host" + e);
} catch (IOException ioe) {
System.out.println("IOException" + ioe);
}
}
public static String readString() throws IOException {
String string = new String("");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
string = br.readLine();
return string;
}
}
服务器端
package cs;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* @Weslysong
*/
private static boolean running = true;
public static void main(String[] args) {
try {
ServerSocket serverservice = new ServerSocket(2545);
System.out.println("server start 0n" + serverservice.getLocalPort());
Socket connection = serverservice.accept();
//接收一个客户端创建一个线程
ServerThread handler = new ServerThread(connection);
} catch (IOException ioe) {
System.out.println("error" + ioe);
}
}
}
线程
package cs;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket _socket) {
this.socket = _socket;
start();
}
public void run() {
try {
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket
.getOutputStream());
String line = new String("");
while (!line.equalsIgnoreCase(". QUIT")) {
line = dis.readUTF();
System.out.println("serveservice:" + line);
dos.writeUTF(line);
dis.close();
dos.close();
socket.close();
}
} catch (IOException ioe) {
System.out.println("connection closed");
}
}
}
package cs;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class Cient {
/**
* @Weslysong
*/
public static void main(String[] args) {
try {
Socket connection = new Socket(args[0], 2545);
System.out.println("建立连接");
DataInputStream dis = new DataInputStream(connection
.getInputStream());
DataOutputStream dos = new DataOutputStream(connection
.getOutputStream());
String line = new String("");
while (!line.toUpperCase().equals("QUIT")) {
System.out.println("enter String:");
line = readString();// 类方法
System.out.println("发送到服务器");
dos.writeUTF(line);
System.out.println("等待服务器相应");
line = dis.readUTF();
System.out.println("服务器:" + line);
}
dis.close();
dos.close();
connection.close();
} catch (UnknownHostException e) { System.out.println("unknow host" + e);
} catch (IOException ioe) {
System.out.println("IOException" + ioe);
}
}
public static String readString() throws IOException {
String string = new String("");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
string = br.readLine();
return string;
}
}
服务器端
package cs;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* @Weslysong
*/
private static boolean running = true;
public static void main(String[] args) {
try {
ServerSocket serverservice = new ServerSocket(2545);
System.out.println("server start 0n" + serverservice.getLocalPort());
Socket connection = serverservice.accept();
//接收一个客户端创建一个线程
ServerThread handler = new ServerThread(connection);
} catch (IOException ioe) {
System.out.println("error" + ioe);
}
}
}
线程
package cs;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket _socket) {
this.socket = _socket;
start();
}
public void run() {
try {
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket
.getOutputStream());
String line = new String("");
while (!line.equalsIgnoreCase(". QUIT")) {
line = dis.readUTF();
System.out.println("serveservice:" + line);
dos.writeUTF(line);
dis.close();
dos.close();
socket.close();
}
} catch (IOException ioe) {
System.out.println("connection closed");
}
}
}
相关文章推荐
- epoll高级应用之同时处理 tcp/udp 的服务器
- Linux TCP server系列(1)-简单TCP服务器+多进程处理客户请求
- Linux TCP server系列(1)-简单TCP服务器+多进程处理客户请求
- 关于从服务器oracle11g上导出的sql (编码格式不同时)再导入到本地oracle11g上时乱码的处理的
- 客户分端--服务器 请求处理模型总结
- Linux网络编程--单进程服务器处理多客户请求(I/O复用)
- 安卓端同时上传文字和图片到服务器,服务器接收并处理
- Url请求长度HTTP协议不对URI的长度作事先的限制,服务器必须能够处理任何他们提供资源的URI,并且应该能够处理无限长度的URIs,这种无效长度的URL可能会在客户端以基于GET方式的请求时产生。
- 如果客户机器无法从DHCP服务器中获取IP地址那么Windows Server 2003客户机器将会如何处理自己的TCP/IP设置
- linux高性能服务器编程之同时处理UDP请求和TCP请求的服务器
- Linux下使用epoll函数同时处理TCP请求和UDP请求的回射服务器
- mac服务器缓慢优化处理(定期重启解决大部分问题)
- WAYOS免拉黑服务器版已开发成功,可在远程同时控制多台WAYOS,全新处理内核
- 处理服务器故障的前5分钟(转)
- 使用 TCP 协议的面向连接的客户-服务器程序设计
- IIS负载均衡ARR前端请求到本地服务器和后端处理服务器
- Servlet是如何处理多个请求同时访问呢?
- 单消息队列客户-服务器间的双向通信
- java网络编程二:服务器处理多个用户请求的解决方案(返回多个用户输入的信息)