gpt4 book ai didi

java - 套接字服务器的可行性

转载 作者:行者123 更新时间:2023-12-01 12:49:17 24 4
gpt4 key购买 nike

我为android客户端设计了一个Socket基础服务器,它接收请求并处理mysql数据库,现在我预计每小时大约2000个请求,我想知道这个服务器是否可以承受这个数量甚至更多?我应该使用其他东西或更好的协议(protocol)来代替套接字来处理更大的请求,但是 ThreadPoolExecutor 吗?

 public class Server {
// a unique ID for each connection
private static int uniqueId;
// an ArrayList to keep the list of the Client
private ArrayList<ClientThread> al;
// if I am in a GUI
private ServerGUI sg;
// to display time
private SimpleDateFormat sdf;
// the port number to listen for connection
private int port;
// the boolean that will be turned of to stop the server
private boolean keepGoing;
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/openfire";
static final String USER = "admin";
static final String PASS = "admin";
static boolean jobdone = false;
protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
protected Thread runningThread= null;


Connection conn = null;
Statement stmt = null;

public Server(int port) {

this(port, null);
}

public Server(int port, ServerGUI sg) {

this.sg = sg;

this.port = port;

sdf = new SimpleDateFormat("HH:mm:ss");

al = new ArrayList<ClientThread>();
}

public void start() throws InterruptedException {
keepGoing = true;
try
{

// the socket used by the server
ServerSocket serverSocket = new ServerSocket(port);

// infinite loop to wait for connections
while(keepGoing)
{
// format message saying we are waiting
display("Server waiting for Clients on port " + port +
".");

Socket socket = serverSocket.accept(); // accept
connection
// if I was asked to stop

if(!keepGoing)
break;
ClientThread t = new ClientThread(socket); // make a
thread of it
jobdone=false;

al.add(t); // save
it in the ArrayList
t.start();
}
// I was asked to stop
try {
serverSocket.close();
for(int i = 0; i < al.size(); ++i) {
ClientThread tc = al.get(i);
try {
tc.sInput.close();
tc.sOutput.close();
tc.socket.close();
}
catch(IOException ioE) {
// not much I can do
}
}
}
catch(Exception e) {
display("Exception closing the server and clients: " + e);
}
}
// something went bad
catch (IOException e) {
String msg = sdf.format(new Date()) + " Exception on new ServerSocket: " + e
+ "\n";
display(msg);
}
}
/*
* For the GUI to stop the server
*/
protected void stop() {
keepGoing = false;
// connect to myself as Client to exit statement
// Socket socket = serverSocket.accept();
try {
new Socket("localhost", port);
}
catch(Exception e) {
// nothing I can really do
}
}
/*
* Display an event (not a message) to the console or the GUI
*/
private void display(String msg) {
String time = sdf.format(new Date()) + " " + msg;
if(sg == null)
System.out.println(time);
else
sg.appendEvent(time + "\n");
}

// create a server object and start it



public static void shutdown() {
jobdone = true;


}
/** One instance of this thread will run for each client */

class ClientThread extends Thread {
// the socket where to listen/talk
String Type;
Socket socket;
ObjectInputStream sInput;
ObjectOutputStream sOutput;
// my unique id (easier for deconnection)
int id;

String encrypted = "'adomgaldkopnfaosdppkdsad'";


// Constructore
ClientThread(Socket socket) throws InterruptedException {
// a unique id
id = ++uniqueId;
this.socket = socket;
/* Creating both Data Stream */
System.out.println("Thread trying to create Object Input/Output
Streams");
while (!jobdone){
try
{
// create output first
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
// read the username


String RegisterRequest = (String) sInput.readObject();

String[] result = RegisterRequest.split("\\,");
String theuser = result[0];
display("theuser="+theuser);
String thepass = result[1];
display("thepass="+thepass);
String thename = result[2];
display("thename="+thename);
String themail = result[3];
display("themail="+themail);
String thephone = result[4];
display("thephone="+thephone);
String newRID = result[5];
display("RID="+newRID);



String OldRID=newRID;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");

//STEP 3: Open a connection
System.out.println("Connecting to a selected
database...");
conn = DriverManager.getConnection(DB_URL, USER,
PASS);
System.out.println("Connected database
successfully...");

String selectSQL1 = "SELECT username FROM ofuser
WHERE username= '"+theuser+"'";
PreparedStatement preparedStatement =
conn.prepareStatement(selectSQL1);
ResultSet usernameRS =
preparedStatement.executeQuery(selectSQL1);


if (!usernameRS.next()){



//STEP 4: Execute a query
System.out.println("Inserting records into the
table...");
stmt = conn.createStatement();

String sql = "INSERT INTO ofuser VALUES

('"+theuser+"','"+thepass+"',null,'"+thename+"','"+themail+"',
'"+4234+"','"+23432+"','"+the
phone+"','"+null+"','"+null+"')";
stmt.executeUpdate(sql);
Type="T";
String REresponse ="('"+Type+"','"+OldRID+"')";
System.out.println(REresponse);
sOutput.writeObject(REresponse);
sOutput.flush();
// sOutput.writeObject(Type);
System.out.println("SUCCESS");
}else{

Type="F";
String REresponse ="('"+Type+"','"+OldRID+"')";
Type="F";
sOutput.writeObject(REresponse);
sOutput.flush();
System.out.println("FAIL");
}



return;




}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}
}//end try






catch (IOException e) {
display("Exception creating new Input/output Streams: " +
e);
return;
}
// have to catch ClassNotFoundException
// but I read a String, I am sure it will work
catch (ClassNotFoundException e) {
}
Thread.sleep(5000);
Server.shutdown();
}

}
// what will run forever

// remove myself from the arrayList containing the list of the
// connected Clients
public void main(String[] args) throws InterruptedException {
// start server on port 1500 unless a PortNumber is specified
int portNumber = 1500;
switch(args.length) {
case 1:
try {
portNumber = Integer.parseInt(args[0]);
}
catch(Exception e) {
System.out.println("Invalid port number.");
System.out.println("Usage is: > java
Server [portNumber]");
return;
}
case 0:
break;
default:
System.out.println("Usage is: > java Server
[portNumber]");
return;

}
// create a server object and start it
Server server = new Server(portNumber);
server.start();
}







// try to close everything
private void close() {
// try to close the connection
try {


if(sOutput != null) sOutput.close();
}
catch(Exception e) {}
try {
if(sInput != null) sInput.close();
}
catch(Exception e) {};
try {
if(socket != null) socket.close();
}
catch (Exception e) {}
}

/*
* Write a String to the Client output stream
*/
private boolean writeMsg(String msg) {
// if Client is still connected send the message to it
if(!socket.isConnected()) {
close();
return false;
}
// write the message to the stream
try {
sOutput.writeObject(msg);
}
// if an error occurs, do not abort just inform the user
catch(IOException e) {
display("Error sending message to " );
display(e.toString());
}
return true;
}
}



//public void main(String[] args) {


// System.out.println("Goodbye!");
// }//end main




}

最佳答案

每小时 2000 次约为每秒 0.55 次。您应该能够在作为服务器运行的手机上处理该速率(假设请求并不复杂)

如果您需要每小时处理多达 100K 个连接和超过 10 亿个请求,TCP 可能就足够了。如果您想要更多,您可以使用 UDP,或购买第二个服务器。更多服务器可能会简单得多;)

注意:您的数据库和/或网络带宽很可能早在 TCP 服务器达到极限之前就已经达到极限。

关于java - 套接字服务器的可行性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24371700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com