944,078 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 8270
  • C# RSS
Feb 13th, 2007
0

Socket Programming...

Expand Post »
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!
C# Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
covertx is offline Offline
21 posts
since May 2006
Feb 14th, 2007
0

Re: Socket Programming...

I'm primarily a java programmer so I might have some capitalization wrong here, but it should get you started.
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jun 18th, 2008
0

Re: Socket Programming...

server
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jacobpauls is offline Offline
2 posts
since Apr 2006
Jun 19th, 2008
0

Re: Socket Programming...

Go with jacobpauls code but use one networkstream in server side and one in client side that solves the problem
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kathirvelmm is offline Offline
7 posts
since Mar 2008
Sep 9th, 2009
0

Re: Socket Programming...

thanks to all.....i will try to work on your suggestions....
Reputation Points: 10
Solved Threads: 0
Light Poster
george_82 is offline Offline
26 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Aero Glass
Next Thread in C# Forum Timeline: How to compare string word by word?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC