public Server(int port) { window = new MainWindow(); window.Show(); window.giveServer(this); this.serverPort = port; serverListener = new TcpListener(getLocalIp(), port); window.setConnectedText("Listening For Clients"); listenForClients(); }
public Server(IPAddress serverIp, int port) { window = new MainWindow(); window.Show(); window.giveServer(this); this.serverIp = serverIp; this.serverPort = port; fileConnection = new TcpClient(); messageConnection = new TcpClient(); window.setConnectedText("Attempting to Connect to: " + serverIp.ToString() + ":" + port); Thread connectToServerThread = new Thread(new ThreadStart(connectToServer)); connectToServerThread.Start(); connectToServerThread.IsBackground = true; }
public void readMessages() { while (messageConnection.Connected) { String message = receiveMessages(); if (message == null) { return; } else if (!sentUsername) { window.setConnectedText("Connected To " + message); sentUsername = true; } else if (message.Substring(0, 4).Equals("file")) { message = message.Substring(4); receiveFileLocation = window.getFileSavePath() + "\\" + message.Substring(0, message.IndexOf('*')); receiveFileSize = long.Parse(message.Substring(message.IndexOf('*') + 1)); Thread receiveFileThread = new Thread(new ThreadStart(receiveFile)); receiveFileThread.Start(); receiveFileThread.IsBackground = true; } else if (message.Substring(0, 4).Equals("fail")) { MessageBox.Show("Failed To Send File: " + filesToSend.ElementAt(0).Substring(0, filesToSend.ElementAt(0).IndexOf('*'))); sending = false; } else if (message.Substring(0, 6).Equals("oksend")) { Thread sendThread = new Thread(delegate() { sendFile(filesToSend.ElementAt(0).Substring(0, filesToSend.ElementAt(0).IndexOf('*'))); }); sendThread.Start(); sendThread.IsBackground = true; } else if (message.Substring(0, 6).Equals("cancel")) { if (sending) { window.sendMessageToUser("Partner canceled download"); } else { window.sendMessageToUser("Partner canceled upload"); } setFilesToSend(null); canceled = true; sending = false; receiving = false; } else if (message.Substring(0, 7).Equals("percent")) { message = message.Substring(7); String percent = message.Substring(0, message.IndexOf('*')); String speed = message.Substring(message.IndexOf('*') + 1); window.sendMessageToUser(percent + "% uploaded " + speed + " KB/s"); } else if (message.Substring(0, 8).Equals("failSend")) { receiving = false; } else if (message.Substring(0, 10).Equals("sendOthers")) { filesToSend.RemoveAt(0); if (filesToSend.Count > 0) { String fileName = filesToSend.ElementAt(0).Substring(filesToSend.ElementAt(0).LastIndexOf("\\") + 1); sendMessage("file" + fileName); } else { setFilesToSend(null); } } } }