static void Main(string[] args) { // myThread = new System.Threading.Thread(new System.Threading.ThreadStart(); while (true) { TcpClient client = service.newClient(); HandleClient handleClient = new HandleClient(); handleClient.startClient(client, service.getClients()); // service.recieve(client); } }
static void Main(string[] args) { int activeClientCount; TcpListener serverSocket = null; TcpClient clientSocket = null; IPAddress serverIP = null; Int32 serverPort = 0; bool customIP = false; bool customPort = false; if (args.Length >= 1) { for (int i = 0; i < args.Length; i++) { string arg = args[i]; switch (arg) { case "-a": try { i++; serverIP = IPAddress.Parse(args[i]); customIP = true; } catch (Exception) { Console.WriteLine("server: >> Wrong IP syntax"); Console.ReadLine(); return; } break; case "-p": try { i++; serverPort = Convert.ToInt32(args[i]); if (serverPort < 1 || serverPort > 65535) { throw new FormatException(); } customPort = true; } catch (Exception) { Console.WriteLine("server: >> Wrong port syntax"); Console.ReadLine(); return; } break; case "--local": try { serverIP = IPAddress.Parse(LOCALHOST_SERVER_IP); customIP = true; } catch (Exception) { Console.WriteLine("server: >> Wrong localhost IP syntax"); Console.ReadLine(); return; } break; case "-h": PrintHelp(); return; case "--help": PrintHelp(); return; } } } if (customIP == false) { try { serverIP = IPAddress.Parse(GetLocalIPAddress()); } catch (Exception) { Console.WriteLine("server: >> Wrong IP syntax"); Console.ReadLine(); return; } } if (customPort == false) { serverPort = DEFAULT_SERVER_PORT; } try { try { // STEP 1: database initilization PrintVerboseMessage("server: >> Server started"); if (File.Exists(DATABASE_NAME) == false) { SQLiteConnection.CreateFile(DATABASE_NAME); PrintVerboseMessage("server: >> Database created"); } DirectoryInfo serverDir = new DirectoryInfo(serverDirPath); if (serverDir.Exists == false) { serverDir.Create(); } PrintVerboseMessage("server: >> Server directory path: " + serverDirPath); DirectoryInfo snapshotDir = new DirectoryInfo(snapshotDirPath); if (snapshotDir.Exists == false) { snapshotDir.Create(); } PrintVerboseMessage("server: >> Server snapshot path: " + snapshotDirPath); DirectoryInfo tmpDir = new DirectoryInfo(tmpDirPath); if (tmpDir.Exists == false) { tmpDir.Create(); } PrintVerboseMessage("server: >> Server tmp path: " + tmpDirPath); PrintVerboseMessage("server: >> Connection with database opening..."); using (SQLiteConnection dbConnection = new SQLiteConnection("Data Source=cloudServerDB.sqlite;Version=3;")) { dbConnection.Open(); DBmanager dbManager = new DBmanager(dbConnection); if (dbManager.dbTableExists("users") == false) { string query = "CREATE TABLE users (username VARCHAR(255) PRIMARY KEY NOT NULL, hash_pwd TEXT NOT NULL, salt TEXT NOT NULL, active INTEGER NOT NULL, backup_path VARCHAR(255) NOT NULL, file_table_name VARCHAR(255) NOT NULL, snapshot_table_name VARCHAR(255) NOT NULL, last_login DATETIME, creation_time DATETIME, last_modification DATETIME);"; int modifNumber = dbManager.dbExecuteWriteQuery(query); if (modifNumber == -1) { throw new Exception("DB table creation error"); } PrintVerboseMessage("server: >> Database 'users' table created"); } PrintVerboseMessage("server: >> Database ready"); } PrintVerboseMessage("server: >> Connection with database closed"); // STEP 2: sockets initialization serverSocket = new TcpListener(serverIP, serverPort); PrintVerboseMessage("server: >> Server IP address: " + serverIP); PrintVerboseMessage("server: >> Server port: " + serverPort); activeClientCount = 0; clientSocket = default(TcpClient); // inizializzazione neutrale serverSocket.Start(); PrintVerboseMessage("server: >> Main listener socket started"); PrintVerboseMessage("server: >> Ready to accept TCP connections from client"); while (true) { try { // STEP 3: connection with a client PrintVerboseMessage("server: >> Waiting for a new client..."); clientSocket = serverSocket.AcceptTcpClient(); PrintVerboseMessage("server: >> Accepted connections from a client"); activeClientCount++; PrintVerboseMessage("server: >> Client number " + activeClientCount + " is connected"); // STEP 4: new client in a new thread HandleClient client = new HandleClient(); client.startClient(clientSocket); } catch (Exception) { if (clientSocket.Connected) { clientSocket.Close(); Console.WriteLine("server: >> Connection with client closed"); } throw; } } } catch (IOException e) { Console.WriteLine("server:Exception >> IOException: " + e.Message); throw; } catch (ArgumentOutOfRangeException e) { Console.WriteLine("server:Exception >> ArgumentOutOfRangeException: " + e.Message); throw; } catch (ArgumentNullException e) { Console.WriteLine("server:Exception >> ArgumentNullException: " + e.Message); throw; } catch (FormatException e) { Console.WriteLine("server:Exception >> FormatException: " + e.Message); throw; } catch (InvalidOperationException e) { Console.WriteLine("server:Exception >> InvalidOperationException: " + e.Message); throw; } catch (SocketException e) { Console.WriteLine("server:Exception >> SocketException: " + e.Message); throw; } catch (Exception e) { Console.WriteLine("server:Exception >> Execution terminated because of exception: " + e.Message); throw; } } catch (Exception) { if (clientSocket.Connected) { clientSocket.Close(); Console.WriteLine("server: >> Connection with client closed"); } } finally { if (clientSocket.Connected) { clientSocket.Close(); PrintVerboseMessage("server: >> Connection with client closed"); } serverSocket.Stop(); PrintVerboseMessage("server: >> Main server socket closed"); PrintVerboseMessage("server: >> Exiting..."); Console.ReadLine(); } }
public void ListenForClients() { TcpListener newServerSocket = new TcpListener(1337); newServerSocket.Start(); Console.WriteLine("Server Started You Can Chat :)"); while (true) { byte[] bytesFrom = new byte[65536]; string dataFromClient = null; int flag = 0; newClientSocket = default(TcpClient); newClientSocket = newServerSocket.AcceptTcpClient(); NetworkStream networkStream = newClientSocket.GetStream(); networkStream.Read(bytesFrom, 0, (int)newClientSocket.ReceiveBufferSize); dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom); dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("Cool")); if (clientsList.Count == 0) { clientsList.Add(dataFromClient, newClientSocket); broadcast(dataFromClient + " Joined ", dataFromClient, false); Console.WriteLine(dataFromClient + " Joined chat room "); HandleClient client = new HandleClient(); client.startClient(newClientSocket, dataFromClient); } else { foreach (KeyValuePair<object, object> Item in clientsList) { if ((String)Item.Key == dataFromClient) { Byte[] lastMessage = null; lastMessage = Encoding.ASCII.GetBytes("ChangeName" + "Cool"); networkStream.Write(lastMessage, 0, lastMessage.Length); networkStream.Flush(); newClientSocket.Close(); flag = 1; break; } else { flag = 0; } } if(flag == 0) { clientsList.Add(dataFromClient, newClientSocket); broadcast(dataFromClient + " Joined ", dataFromClient, false); Console.WriteLine(dataFromClient + " Joined chat room "); HandleClient client = new HandleClient(); client.startClient(newClientSocket, dataFromClient); } } } newClientSocket.Close(); newServerSocket.Stop(); Console.WriteLine("exit"); Console.ReadLine(); }