| | |
Socket Programming...
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 21
Reputation:
Solved Threads: 0
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!
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!
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)
(IPAddress ipaddr, int port)
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!
+[->,----------]<[+++++++++++.<]
•
•
Join Date: Mar 2004
Posts: 763
Reputation:
Solved Threads: 38
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)
//connect to server and get intput/output stream TcpClient client = new TcpClient(string ip, int port); NetworkStream stream = client.GetStream(); //send a string to the server ASCIIEncoding Enc = new ASCIIEncoding(); string s = "message"; byte[] buffer = Enc.GetBytes(s); stream.Write(buffer, 0, buffer.Length); stream.flush();
Last edited by Phaelax; Feb 14th, 2007 at 4:59 pm.
•
•
Join Date: Apr 2006
Posts: 2
Reputation:
Solved Threads: 0
server
change the ip address to ur ipaddress
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; using System.Security.Cryptography; using ClassLibrary1; public class serv { public static void Main() { //Establishing a connection with the client IPAddress ipAd = IPAddress.Parse("10.0.0.182"); TcpListener newList = new TcpListener(ipAd, 7777); /* Start Listeneting at the specified port */ newList.Start(); Console.WriteLine("The server is running at port 7777..."); Console.WriteLine("Waiting for a connection....."); Socket s = newList.AcceptSocket(); Console.WriteLine("Connection accepted from " + s.RemoteEndPoint); //Accepting the data from the client in byte form byte[] b = new byte[100]; int k = s.Receive(b); Console.WriteLine("Recieved..."); for (int i = 0; i < k; i++) //displaying the data in characters Console.Write(Convert.ToChar(b[i])); //Displaying the data in bytes Console.WriteLine("The byte data is"); for (int i = 0; i < k; i++) Console.Write(b[i]); //Sending acknowlegement to the client ASCIIEncoding asen = new ASCIIEncoding(); s.Send(asen.GetBytes("The string was recieved by the server.")); Console.WriteLine("\nSent Acknowledgement"); //Accepting data using the network streams NetworkStream nts = new NetworkStream(s); StreamReader strea = new StreamReader(nts); StreamWriter strwri = new StreamWriter(nts); string output; output = strea.ReadLine(); Console.WriteLine(output); try { string output2; //Copying the contents of the file output2 = strea.ReadLine(); Console.WriteLine(output2); string dest = ("C://Project//Copy//copy.txt"); File.Copy(output2, dest, true); Console.WriteLine("The file has been copied to the new destination"); //Getting the contents of the copied file Console.WriteLine(); Console.WriteLine("The contents of the Copied file are"); Console.WriteLine(File.ReadAllText("C://Project//Copy//copy.txt")); //Encrypting a file TripleDESCryptoServiceProvider cypto = new TripleDESCryptoServiceProvider(); FileStream filestream = File.Create("C://Project//Copy//secret.txt"); CryptoStream cryptoStream = new CryptoStream(filestream, cypto.CreateEncryptor(), CryptoStreamMode.Write); StreamWriter write = new StreamWriter(cryptoStream); write.WriteLine(File.ReadAllText("C://Project//Copy//copy.txt")); write.Close(); filestream = File.Create("C://Project//Copy//secret.txt"); BinaryWriter binwrite = new BinaryWriter(filestream); binwrite.Write(cypto.Key); binwrite.Write(cypto.IV); binwrite.Close(); Console.WriteLine(); Console.WriteLine("The data is encrypted and stored"); Console.Write("The encrypted data is : "); Console.WriteLine(File.ReadAllText("C:\\Project\\Copy\\Secret.txt")); } catch (FileNotFoundException e) { Console.WriteLine(e); } s.Close(); } } client using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; public class clnt { public static void Main(string[] args) { try { //Establishing the connection TcpClient tcpclnt = new TcpClient(); Console.WriteLine("Connecting....."); tcpclnt.Connect("10.0.0.182", 7777); Console.WriteLine("Connected"); //Transfering of data in the form of bytes Console.Write("Enter the string to be transmitted : "); String str = Console.ReadLine(); Stream stm = tcpclnt.GetStream(); ASCIIEncoding asen = new ASCIIEncoding(); byte[] ba = asen.GetBytes(str); Console.WriteLine("Transmitting....."); stm.Write(ba, 0, ba.Length); byte[] bb = new byte[100]; int k = stm.Read(bb, 0, 100); for (int i = 0; i < k; i++) Console.Write(Convert.ToChar(bb[i])); //Using the network stream to send data NetworkStream nts = tcpclnt.GetStream(); StreamReader strread = new StreamReader(nts); StreamWriter strwrite = new StreamWriter(nts); //Sending a pre-stored text data string str1 = "goodboy"; strwrite.WriteLine(str1); strwrite.Flush(); //Transfering of the file name using the streams Console.WriteLine("Enter the file path"); string str2 = Console.ReadLine(); strwrite.WriteLine(str2); strwrite.Flush(); tcpclnt.Close(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } } }
Last edited by John A; Sep 12th, 2009 at 12:18 am. Reason: added code tags
![]() |
Similar Threads
- About socket programming in php (PHP)
- C++ socket programming (C++)
- How to send a double value in socket programming (C)
- Please help me with CSocket programming (C++)
- Socket (C++)
- Socket programming + porting Unix to Win32 (C)
Other Threads in the C# Forum
- Previous Thread: Aero Glass
- Next Thread: How to compare string word by word?
| Thread Tools | Search this Thread |
.net access algorithm angle array barchart bitmap box broadcast c# capturing check checkbox client combobox control conversion convert csharp custom database datagrid datagridview dataset datetime dbconnection degrees delegate design development disappear draganddrop drawing encryption enum event excel file firefox form format forms function gdi+ httpwebrequest image index input install java label leak libraries list listbox mandelbrot math monodevelop mouseclick msword mysql operator path pause photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox round server sleep socket sql statistics stream string table tcpclientchannel text textbox thread time timer update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml





