Guest User

Untitled

a guest
May 15th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. static class Program
  2. {
  3. public static Form1 mainForm;
  4.  
  5. [STAThread]
  6. static void Main()
  7. {
  8. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  9. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  10.  
  11. Application.EnableVisualStyles();
  12. Application.SetCompatibleTextRenderingDefault(false);
  13.  
  14. mainForm = new Form1();
  15.  
  16. Application.Run(mainForm);
  17. }
  18.  
  19. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  20. {
  21. CrashReporter ErrorDlg = new CrashReporter((Exception)e.ExceptionObject);
  22. ErrorDlg.ShowDialog();
  23. }
  24.  
  25. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  26. {
  27. CrashReporter ErrorDlg = new CrashReporter(e.Exception);
  28. ErrorDlg.ShowDialog();
  29. }
  30. }
  31.  
  32. void connect(string user, string password)
  33. {
  34. if (!_conn)
  35. {
  36. conn = true;
  37. user = user;
  38. pass = password;
  39. tcpThread = new Thread(new ThreadStart(SetupConn));
  40. tcpThread.Start();
  41. imUsername = user;
  42. }
  43. }
  44.  
  45. void SetupConn()
  46. {
  47. client = new TcpClient(Server, Port); // Connect to the server.
  48. netStream = client.GetStream();
  49. ssl = new SslStream(netStream, false, new RemoteCertificateValidationCallback(ValidateCert));
  50. ssl.AuthenticateAsClient("AppNameHere");
  51.  
  52. br = new BinaryReader(ssl, Encoding.UTF8);
  53. bw = new BinaryWriter(ssl, Encoding.UTF8);
  54.  
  55. int connectiong = br.ReadInt32();
  56. if (connectiong == IM_Connecting)
  57. {
  58. WebClient client = new WebClient();
  59. string ipadresa = Base64Encode(client.DownloadString("http://-deleted-for-public-"));
  60.  
  61.  
  62. bw.Write(IM_Connecting);
  63. bw.Write(IM_Login);
  64. bw.Write(UserName);
  65. bw.Write(Password);
  66. bw.Write(ipadresa);
  67. bw.Flush();
  68.  
  69. byte ans = br.ReadByte();
  70. if (ans == IM_OK)
  71. {
  72. OnLoginOK();
  73. Receiver();
  74. }
  75. else
  76. {
  77. IMErrorEventArgs err = new IMErrorEventArgs((IMError)ans);
  78. OnLoginFailed(err);
  79. }
  80. }
  81. if (_conn)
  82. CloseConn();
  83. }
  84.  
  85. public void ImOnline(string user)
  86. {
  87. if (conn) {
  88. if(logged == true) {
  89. bw.Write(IM_Online);
  90. bw.Write(user);
  91. bw.Flush();
  92. }
  93. }
  94. }
  95.  
  96. public void Receiver()
  97. {
  98. logged = true;
  99.  
  100. try
  101. {
  102. while (client.Connected)
  103. {
  104. byte type = br.ReadByte();
  105. if (type == IM_Here) {
  106. string f_online = br.ReadString();
  107. string f_username = br.ReadString();
  108.  
  109. if (f_online == "True") {
  110. friendOnline(true, f_username, "-");
  111. } else {
  112. friendOnline(false, f_username, "-");
  113. }
  114. }
  115. }
  116. }
  117. catch (IOException ex)
  118. {
  119. //MessageBox.Show(ex.ToString());
  120. return;
  121. }
  122.  
  123. logged = false;
  124. }
  125.  
  126. new Thread(() =>
  127. {
  128. TalkForm a = new TalkForm(im, username, dname, mydisplayname);
  129. a.sendTo = username;
  130. chats.Add(a); // Add form to the list of chat forms so it can be found if is already open so it can be 'BringToFront'
  131. a.ShowDialog();
  132.  
  133. }).Start();
Add Comment
Please, Sign In to add comment