Guest User

Untitled

a guest
Aug 31st, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. Application stops when receiving info through socket
  2. private void btnLogin_Click(object sender, EventArgs e)
  3. {
  4. String user = txtUser.Text;
  5. String pass = txtPass.Text;
  6.  
  7. ClientConnectionHandler handler = ClientConnectionHandler.getInstance();
  8. handler.sendMessage("#login#" + user + " " + pass + "#");
  9. User u = (User) handler.receive();
  10.  
  11.  
  12.  
  13. if (u == null)
  14. {
  15. MessageBox.Show("Username/Password is wrong");
  16. }
  17. else
  18. {
  19. if (u.getRang().Equals("admin"))
  20. {
  21.  
  22. (new AdminWin(u)).Show();
  23. this.Hide();
  24. }
  25. else{
  26. (new ClientWin(u)).Show();
  27. this.Hide();
  28. }
  29. }
  30. handler.kill();
  31.  
  32. }
  33.  
  34. public partial class AdminWin : Form
  35. {
  36. private User user;
  37.  
  38. public AdminWin(User u)
  39. {
  40. user = u;
  41. InitializeComponent();
  42.  
  43. ClientConnectionHandler handler = ClientConnectionHandler.getInstance();
  44. handler.sendMessage("#getClientList# #");
  45.  
  46. handler.receive();
  47.  
  48. //listUsers.DataSource = users;
  49.  
  50. }
  51.  
  52.  
  53. }
  54.  
  55. public class ClientConnectionHandler
  56. {
  57. private static ClientConnectionHandler INSTANCE;
  58. private static Socket socket;
  59.  
  60. private ClientConnectionHandler()
  61. {
  62. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  63. IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
  64. socket.Connect("127.0.0.1", 1234);
  65. }
  66.  
  67. public static ClientConnectionHandler getInstance()
  68. {
  69. if (INSTANCE == null)
  70. INSTANCE = new ClientConnectionHandler();
  71. return INSTANCE;
  72. }
  73.  
  74. public void sendMessage(String message)
  75. {
  76. byte[] buffer = new byte[1024];
  77. IFormatter formatter = new BinaryFormatter();
  78. Stream stream = new MemoryStream(buffer);
  79.  
  80. formatter.Serialize(stream, message);
  81. stream.Flush();
  82. socket.Send(buffer, buffer.Length, 0);
  83. }
  84.  
  85. public Object receive()
  86. {
  87. byte[] buffer = new byte[10240];
  88. socket.Receive(buffer);
  89. return toObject(buffer);
  90. }
  91.  
  92. private Object toObject(byte[] byteArray)
  93. {
  94. MemoryStream memStream = new MemoryStream();
  95. BinaryFormatter binForm = new BinaryFormatter();
  96. memStream.Write(byteArray, 0, byteArray.Length);
  97. memStream.Seek(0, SeekOrigin.Begin);
  98. Object obj = (Object)binForm.Deserialize(memStream);
  99. return obj;
  100. }
  101.  
  102. public void kill()
  103. {
  104. socket.Close();
  105. }
  106. }
  107.  
  108. class Server
  109. {
  110. public static void Main()
  111. {
  112. IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1234);
  113. Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  114. socket.Bind(ipEndPoint);
  115. socket.Listen(100);
  116. Console.WriteLine("Server Started");
  117.  
  118. while (true)
  119. {
  120. Socket clientSocket = socket.Accept();
  121. clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  122. Thread clientThread = new Thread(new ThreadStart(new ServerConnectionHandler(clientSocket).handle));
  123. clientThread.Start();
  124. }
  125. }
  126.  
  127. }
  128.  
  129. class ServerConnectionHandler
  130. {
  131. private Socket socket;
  132.  
  133. public ServerConnectionHandler(Socket socket)
  134. {
  135. this.socket = socket;
  136. }
  137.  
  138. public void handle()
  139. {
  140. byte[] data = new byte[1024];
  141. int receivedDataLength = socket.Receive(data);
  142. String stringData = new ASCIIEncoding().GetString(data);
  143.  
  144. stringData = stringData.Substring(stringData.IndexOf("#"));
  145.  
  146. Console.WriteLine(stringData);
  147.  
  148. string[] bySharp = stringData.Split('#');
  149.  
  150. string action = bySharp[1];
  151. string info = bySharp[2];
  152.  
  153. Console.WriteLine(action + " " + info);
  154.  
  155. switch (action)
  156. {
  157. case "login": handleLogin(info); break;
  158. case "getClientList": handleClientList(); break;
  159. case "getCDsForClient": handleCDList(info); break;
  160. case "addCDForClient": handleAdd(info); break;
  161. case "remCD": handleRem(info); break;
  162. case "modCD": handleMod(info); break;
  163. }
  164. }
  165.  
  166. private void handleLogin(string info)
  167. {
  168. string[] bySpace = info.Split(' ');
  169. string user = bySpace[0];
  170. string pass = bySpace[1];
  171.  
  172. User u = RepositoryManager.getInstance().getUser(user, pass);
  173.  
  174. sendToClient(toByteArray(u));
  175. }
  176.  
  177. private void handleClientList()
  178. {
  179. sendToClient(toByteArray(RepositoryManager.getInstance().getClientList()));
  180. }
  181.  
  182. private void handleCDList(string info)
  183. {
  184. long userId = long.Parse(info);
  185. sendToClient(toByteArray(RepositoryManager.getInstance().getCDs(userId)));
  186. }
  187.  
  188. private void handleAdd(string info)
  189. {
  190. string[] byTilda = info.Split('~');
  191.  
  192. long userId = long.Parse(byTilda[0]);
  193. String cdName = byTilda[1];
  194. String cdType = byTilda[2];
  195. RepositoryManager.getInstance().addCD(userId, cdName,
  196. cdType);
  197. }
  198.  
  199. private void handleRem(string info)
  200. {
  201. string[] bySpace = info.Split(' ');
  202. long userId = long.Parse(bySpace[0]);
  203. long cdId = long.Parse(bySpace[1]);
  204. RepositoryManager.getInstance().remCD(userId, cdId);
  205. }
  206.  
  207. private void handleMod(string info)
  208. {
  209. string[] byTilda = info.Split('~');
  210. long userId = long.Parse(byTilda[0]);
  211. long cdId = long.Parse(byTilda[1]);
  212. String newName = byTilda[2];
  213. String newType = byTilda[3];
  214.  
  215. RepositoryManager.getInstance().modCD(userId, cdId,
  216. newName, newType);
  217. }
  218.  
  219. private void sendToClient(byte[] info)
  220. {
  221. socket.Send(info, info.Length, 0);
  222. }
  223.  
  224. private byte[] toByteArray(Object o)
  225. {
  226. BinaryFormatter bf = new BinaryFormatter();
  227. MemoryStream ms = new MemoryStream();
  228. bf.Serialize(ms, o);
  229. return ms.ToArray();
  230. }
  231. }
Add Comment
Please, Sign In to add comment