private void button2_Click(object sender, EventArgs e) { try { if (_clientSocket.Connected) { HTTPData jdata = new HTTPData() { Body = new Dictionary <string, string>(), Header = "Disconnect", Auth = GlobalData.Auth }; GlobalData.sendJSONdata(jdata); _clientSocket.Shutdown(SocketShutdown.Both); _clientSocket.Close(); if (!_clientSocket.Connected) { label1.Text = "Connected to: None"; label1.ForeColor = Color.Red; //GlobalData.socket = null; //MessageBox.Show("Disconnected!", "Disconnect message"); _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } else { MessageBox.Show("Failed to Disconnect.. :(", "Uh Oh!"); } } else { _clientSocket.Shutdown(SocketShutdown.Both); _clientSocket.Close(); } } catch (Exception ex) { //_clientSocket.Shutdown(SocketShutdown.Both); //_clientSocket.Close(); //if (!_clientSocket.Connected) //{ // label1.Text = "Connected to: None"; // label1.ForeColor = Color.Red; // GlobalData.socket = null; // //MessageBox.Show("Disconnected!", "Disconnect message"); // _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //} //else //{ // MessageBox.Show("Failed to Disconnect.. :(", "Uh Oh!"); //} } }
private void button1_Click(object sender, EventArgs e) { //if(GlobalData.socket is null) { try { if (comboBox1.SelectedItem != null) { KeyValuePair <string, string> KVP = (KeyValuePair <string, string>)comboBox1.SelectedItem; if (KVP.Value != null) { _clientSocket.Connect(KVP.Value, 7878); HTTPData jdata = new HTTPData() { Auth = GlobalData.Auth, Header = "Connect", Body = new Dictionary <string, string>() }; jdata.Body.Add("Discord_Username", GlobalData.Username); GlobalData.sendJSONdata(jdata); label1.Text = "Connected to: " + KVP.Key; label1.ForeColor = Color.Green; _clientSocket.ReceiveTimeout = 5000; byte[] ResponceBuff = new byte[1024]; int lng = 0; try { lng = _clientSocket.Receive(ResponceBuff); } catch (Exception ex) { MessageBox.Show("Server Timed out!, Disconnecting!", "Uh Oh!", MessageBoxButtons.OK); GlobalData.ErrorLogInput(ex, "Sever Time Out!"); _clientSocket.Disconnect(true); return; } byte[] recBytes = new byte[lng]; Array.Copy(ResponceBuff, recBytes, lng); string responce = Encoding.ASCII.GetString(recBytes); HTTPData responceData = JsonConvert.DeserializeObject <HTTPData>(responce); if (responceData.Auth == GlobalData.Auth) { if (responceData.Header == "Success_Connect") { LRMServerData serv = new LRMServerData() { Name = responceData.Body.FirstOrDefault(x => x.Key == "Name").Value, Type = responceData.Body.FirstOrDefault(x => x.Key == "Type").Value, PlayerCount = responceData.Body.FirstOrDefault(x => x.Key == "Players").Value, //serverSocket = _clientSocket, }; } else { MessageBox.Show("Server Responded with an Invalad Responce!", "Uh Oh!", MessageBoxButtons.OK); } } else { MessageBox.Show("Server Responded with an Invalad Responce!", "Uh Oh!", MessageBoxButtons.OK); } } } } catch (Exception ex) { GlobalData.ErrorLogInput(ex, "ERROR"); MessageBox.Show("Error: " + ex.Message, "Uh Oh!"); } } }