Click here to Skip to main content
15,906,267 members
Home / Discussions / C#
   

C#

 
GeneralRe: New #dev....again [edit] Pin
leppie25-Oct-03 9:22
leppie25-Oct-03 9:22 
GeneralRe: New #dev....again [edit] Pin
J. Dunlap25-Oct-03 9:30
J. Dunlap25-Oct-03 9:30 
GeneralRe: New #dev....again [edit] Pin
leppie25-Oct-03 9:37
leppie25-Oct-03 9:37 
GeneralRe: New #dev....again [edit] Pin
J. Dunlap25-Oct-03 9:51
J. Dunlap25-Oct-03 9:51 
GeneralRe: New #dev....again [edit] Pin
Blake Coverett26-Oct-03 2:07
Blake Coverett26-Oct-03 2:07 
GeneralC# Network... Need Suggestions. Pin
fadee24-Oct-03 22:35
fadee24-Oct-03 22:35 
GeneralRe: C# Network... Need Suggestions. Pin
Blake Coverett25-Oct-03 20:15
Blake Coverett25-Oct-03 20:15 
GeneralRe: C# Network... Need Suggestions. Pin
Blake Coverett25-Oct-03 23:02
Blake Coverett25-Oct-03 23:02 
Here is a naive implementation. A more robust solution needs to add exception handling, threading and checksums, but this should get you started.

Note: I wouldn't recommend using TcpListener and TcpClient at all, the async methods on Socket are the way to go, but you said that's what you said you were using, so that's what I used below.

Server.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;

class Server {
	static int Main(string[] args)
	{
		TcpListener listener = new TcpListener(IPAddress.Any, 0xF00);
		listener.Start();
		using (TcpClient client = listener.AcceptTcpClient()) {
			NetworkStream stream = client.GetStream();
			StreamWriter writer = new StreamWriter(stream);
			StreamReader reader = new StreamReader(stream);
			string line;

			writer.WriteLine("FileSender:1.0");
			writer.Flush();
			if ((line = reader.ReadLine()) != "FileReciever:1.0") {
				Console.WriteLine("Unrecognized Client:{0}", line);
				return 1;
			}

			foreach (string fileName in args) {
				FileStream file = new FileStream(fileName, FileMode.Open);
				byte[] buffer = new byte[file.Length];
				string fileSize = file.Length.ToString();
				file.Read(buffer, 0, (int)file.Length);

				writer.WriteLine("FileName:{0}\r\nFileSize:{1}", 
					fileName, fileSize);
				writer.Flush();

				if ((line = reader.ReadLine()) != "Send:" + fileSize) {
					Console.WriteLine("Protocol Error:{0}", line);
					return 1;
				}

				stream.Write(buffer, 0, (int)file.Length);

				if ((line = reader.ReadLine()) != "Recieved:" + fileSize) {
					Console.WriteLine("Send {0} Failed:{1}", fileName, line);
					return 1;
				} else {
					Console.WriteLine("Send {0} Complete", fileName);
				}
			}
		}
		return 0;
	}
}


client.cs
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;

class Client {
	static int Main(string[] args)
	{
		using (TcpClient client = new TcpClient(args[0], 0xF00)) {
			NetworkStream stream = client.GetStream();
			StreamWriter writer = new StreamWriter(stream);
			StreamReader reader = new StreamReader(stream);
			string line;

			if ((line = reader.ReadLine()) != "FileSender:1.0") {
				Console.WriteLine("Unrecognized Server:{0}", line);
				return 1;
			}
			writer.WriteLine("FileReciever:1.0");
			writer.Flush();

			while ((line = reader.ReadLine()) != null) {
				if (!line.StartsWith("FileName:")) {
					Console.WriteLine("Expected FileName:{0}", line);
					return 1;
				}
				string fileName = line.Substring(9);

				line = reader.ReadLine();
				if (!line.StartsWith("FileSize:")) {
					Console.WriteLine("Expected FileSize:{0}", line);
					return 1;
				}
				int fileSize = int.Parse(line.Substring(9));

				byte[] buffer = new byte[fileSize];

				using (FileStream file = 
					new FileStream(fileName, FileMode.CreateNew)) {

					writer.WriteLine("Send:{0}", fileSize);
					writer.Flush();

					stream.Read(buffer, 0, fileSize);
					file.Write(buffer, 0, fileSize);
				}

				writer.WriteLine("Recieved:{0}", fileSize);
				writer.Flush();
			}
		}
		return 0;
	}
}


Compile and then run 'server test1.dat test2.dat' in one window, and 'client localhost' in another.

-Blake
GeneralRe: C# Network... Need Suggestions. Pin
fadee25-Oct-03 23:30
fadee25-Oct-03 23:30 
GeneralRe: C# Network... Need Suggestions. Pin
Blake Coverett26-Oct-03 0:19
Blake Coverett26-Oct-03 0:19 
GeneralRe: C# Network... Need Suggestions. Pin
Blake Coverett26-Oct-03 2:05
Blake Coverett26-Oct-03 2:05 
GeneralRe: C# Network... Need Suggestions. Pin
fadee26-Oct-03 6:03
fadee26-Oct-03 6:03 
GeneralRe: C# Network... Need Suggestions. Pin
Blake Coverett26-Oct-03 6:12
Blake Coverett26-Oct-03 6:12 
GeneralRe: C# Network... Need Suggestions. Pin
fadee26-Oct-03 6:22
fadee26-Oct-03 6:22 
Generalstatic variable Pin
hkl24-Oct-03 10:43
hkl24-Oct-03 10:43 
GeneralRe: static variable Pin
Guillermo Rivero24-Oct-03 10:57
Guillermo Rivero24-Oct-03 10:57 
GeneralRe: static variable Pin
hkl24-Oct-03 10:58
hkl24-Oct-03 10:58 
GeneralRe: static variable Pin
Rocky Moore25-Oct-03 2:48
Rocky Moore25-Oct-03 2:48 
GeneralRe: static variable Pin
leppie25-Oct-03 3:15
leppie25-Oct-03 3:15 
GeneralRe: static variable Pin
hkl27-Oct-03 8:33
hkl27-Oct-03 8:33 
GeneralGenerating a unique serial Pin
Sassan K.Z.24-Oct-03 8:30
Sassan K.Z.24-Oct-03 8:30 
GeneralRe: Generating a unique serial Pin
Guillermo Rivero24-Oct-03 8:35
Guillermo Rivero24-Oct-03 8:35 
GeneralRe: Generating a unique serial Pin
Sassan K.Z.24-Oct-03 9:41
Sassan K.Z.24-Oct-03 9:41 
GeneralRe: Generating a unique serial Pin
leppie25-Oct-03 2:00
leppie25-Oct-03 2:00 
GeneralRe: Generating a unique serial Pin
mistery2225-Oct-03 2:26
mistery2225-Oct-03 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.