Socket Programming...

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 21
Reputation: covertx is an unknown quantity at this point 
Solved Threads: 0
covertx covertx is offline Offline
Newbie Poster

Socket Programming...

 
0
  #1
Feb 13th, 2007
Hi all, I've been working on a client/server program for a good while now and can't understand the online help libraries offered from the MSDN site!

I am using the Visual C# 8 Express version for .NET.

The user enters information into the client side applications and presses a send button. The message then gets sent to the server side application. The server-side application is activated when server user activates the server with a start button...

The trouble I am having is many of the functions I have found online for socket programming are deprecated and so cannot be used for my application...

I've found some other solutionsto the problem but they are presented in a way that I really don't understand!
  1. (IPAddress ipaddr, int port)
Would anyone be able to tell me how I can send a simple string from a client application to a listening server application?

FYI, I'm developing both the client and the server applications on my local machine.

Thanks for reading, and thanks in advance for any help given!
+[->,----------]<[+++++++++++.<]
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Socket Programming...

 
0
  #2
Feb 14th, 2007
I'm primarily a java programmer so I might have some capitalization wrong here, but it should get you started.
  1. //connect to server and get intput/output stream
  2. TcpClient client = new TcpClient(string ip, int port);
  3. NetworkStream stream = client.GetStream();
  4.  
  5. //send a string to the server
  6. ASCIIEncoding Enc = new ASCIIEncoding();
  7. string s = "message";
  8. byte[] buffer = Enc.GetBytes(s);
  9. stream.Write(buffer, 0, buffer.Length);
  10. stream.flush();
Last edited by Phaelax; Feb 14th, 2007 at 4:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 2
Reputation: jacobpauls is an unknown quantity at this point 
Solved Threads: 0
jacobpauls jacobpauls is offline Offline
Newbie Poster

Re: Socket Programming...

 
0
  #3
Jun 18th, 2008
server
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.IO;
  7. using System.Security.Cryptography;
  8. using ClassLibrary1;
  9.  
  10. public class serv
  11. {
  12. public static void Main()
  13. {
  14.  
  15. //Establishing a connection with the client
  16. IPAddress ipAd = IPAddress.Parse("10.0.0.182");
  17. TcpListener newList = new TcpListener(ipAd, 7777);
  18. /* Start Listeneting at the specified port */
  19.  
  20. newList.Start();
  21. Console.WriteLine("The server is running at port 7777...");
  22. Console.WriteLine("Waiting for a connection.....");
  23. Socket s = newList.AcceptSocket();
  24. Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
  25.  
  26.  
  27. //Accepting the data from the client in byte form
  28. byte[] b = new byte[100];
  29. int k = s.Receive(b);
  30. Console.WriteLine("Recieved...");
  31. for (int i = 0; i < k; i++)
  32. //displaying the data in characters
  33. Console.Write(Convert.ToChar(b[i]));
  34.  
  35. //Displaying the data in bytes
  36. Console.WriteLine("The byte data is");
  37. for (int i = 0; i < k; i++)
  38. Console.Write(b[i]);
  39. //Sending acknowlegement to the client
  40. ASCIIEncoding asen = new ASCIIEncoding();
  41. s.Send(asen.GetBytes("The string was recieved by the server."));
  42. Console.WriteLine("\nSent Acknowledgement");
  43.  
  44. //Accepting data using the network streams
  45. NetworkStream nts = new NetworkStream(s);
  46. StreamReader strea = new StreamReader(nts);
  47. StreamWriter strwri = new StreamWriter(nts);
  48. string output;
  49. output = strea.ReadLine();
  50. Console.WriteLine(output);
  51.  
  52. try
  53. {
  54. string output2;
  55. //Copying the contents of the file
  56. output2 = strea.ReadLine();
  57. Console.WriteLine(output2);
  58. string dest = ("C://Project//Copy//copy.txt");
  59. File.Copy(output2, dest, true);
  60. Console.WriteLine("The file has been copied to the new destination");
  61.  
  62. //Getting the contents of the copied file
  63. Console.WriteLine();
  64. Console.WriteLine("The contents of the Copied file are");
  65. Console.WriteLine(File.ReadAllText("C://Project//Copy//copy.txt"));
  66.  
  67. //Encrypting a file
  68. TripleDESCryptoServiceProvider cypto = new TripleDESCryptoServiceProvider();
  69. FileStream filestream = File.Create("C://Project//Copy//secret.txt");
  70.  
  71. CryptoStream cryptoStream = new CryptoStream(filestream, cypto.CreateEncryptor(), CryptoStreamMode.Write);
  72. StreamWriter write = new StreamWriter(cryptoStream);
  73. write.WriteLine(File.ReadAllText("C://Project//Copy//copy.txt"));
  74. write.Close();
  75.  
  76. filestream = File.Create("C://Project//Copy//secret.txt");
  77. BinaryWriter binwrite = new BinaryWriter(filestream);
  78. binwrite.Write(cypto.Key);
  79. binwrite.Write(cypto.IV);
  80. binwrite.Close();
  81. Console.WriteLine();
  82. Console.WriteLine("The data is encrypted and stored");
  83. Console.Write("The encrypted data is : ");
  84. Console.WriteLine(File.ReadAllText("C:\\Project\\Copy\\Secret.txt"));
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. }
  92. catch (FileNotFoundException e)
  93. {
  94. Console.WriteLine(e);
  95. }
  96.  
  97.  
  98.  
  99.  
  100. s.Close();
  101.  
  102.  
  103.  
  104.  
  105.  
  106. }
  107. }
  108.  
  109.  
  110. client
  111.  
  112. using System;
  113. using System.Collections.Generic;
  114. using System.Text;
  115.  
  116. using System.IO;
  117. using System.Net;
  118.  
  119. using System.Net.Sockets;
  120.  
  121.  
  122. public class clnt
  123. {
  124.  
  125. public static void Main(string[] args)
  126. {
  127.  
  128. try
  129. {
  130. //Establishing the connection
  131. TcpClient tcpclnt = new TcpClient();
  132. Console.WriteLine("Connecting.....");
  133. tcpclnt.Connect("10.0.0.182", 7777);
  134. Console.WriteLine("Connected");
  135.  
  136.  
  137. //Transfering of data in the form of bytes
  138. Console.Write("Enter the string to be transmitted : ");
  139. String str = Console.ReadLine();
  140. Stream stm = tcpclnt.GetStream();
  141. ASCIIEncoding asen = new ASCIIEncoding();
  142. byte[] ba = asen.GetBytes(str);
  143. Console.WriteLine("Transmitting.....");
  144. stm.Write(ba, 0, ba.Length);
  145. byte[] bb = new byte[100];
  146. int k = stm.Read(bb, 0, 100);
  147. for (int i = 0; i < k; i++)
  148. Console.Write(Convert.ToChar(bb[i]));
  149.  
  150.  
  151. //Using the network stream to send data
  152. NetworkStream nts = tcpclnt.GetStream();
  153. StreamReader strread = new StreamReader(nts);
  154. StreamWriter strwrite = new StreamWriter(nts);
  155. //Sending a pre-stored text data
  156. string str1 = "goodboy";
  157. strwrite.WriteLine(str1);
  158. strwrite.Flush();
  159.  
  160.  
  161.  
  162. //Transfering of the file name using the streams
  163. Console.WriteLine("Enter the file path");
  164. string str2 = Console.ReadLine();
  165. strwrite.WriteLine(str2);
  166. strwrite.Flush();
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. tcpclnt.Close();
  177.  
  178. }
  179.  
  180. catch (Exception e)
  181. {
  182. Console.WriteLine("Error..... " + e.StackTrace);
  183. }
  184. }
  185.  
  186. }
change the ip address to ur ipaddress
Last edited by John A; Sep 12th, 2009 at 12:18 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: kathirvelmm is an unknown quantity at this point 
Solved Threads: 0
kathirvelmm kathirvelmm is offline Offline
Newbie Poster

Re: Socket Programming...

 
0
  #4
Jun 19th, 2008
Go with jacobpauls code but use one networkstream in server side and one in client side that solves the problem
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: george_82 is an unknown quantity at this point 
Solved Threads: 0
george_82 george_82 is offline Offline
Newbie Poster

Re: Socket Programming...

 
0
  #5
Sep 9th, 2009
thanks to all.....i will try to work on your suggestions....
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC