Click here to Skip to main content
12,358,747 members (61,867 online)
Click here to Skip to main content

Stats

365.9K views
37.1K downloads
201 bookmarked
Posted

A TCP/IP Server written in C#

, 29 Dec 2003 CPOL
TCP/IP Server, which is running as a Windows Service waiting for client requests
using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections;

namespace TCPClient
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class TCPClient
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			TCPClient client = null;
			client = new TCPClient("SatyaTest.cfg\r\n");
			client = new TCPClient("SatyaTest1.cfg\r\n");
			client = new TCPClient("SatyaTest2.cfg\r\n");
			client = new TCPClient("SatyaTest3.cfg\r\n");
			client = new TCPClient("SatyaTest4.cfg\r\n");
			client = new TCPClient("SatyaTest5.cfg\r\n");
		}

		private String m_fileName=null;
		public TCPClient(String fileName)
		{
			m_fileName=fileName;
			Thread t = new Thread(new ThreadStart(ClientThreadStart));
			t.Start();
		}

		private void ClientThreadStart()
		{
			Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
			clientSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"),31001));

			// Send the file name.
			clientSocket.Send(Encoding.ASCII.GetBytes(m_fileName));
			
			// Receive the length of the filename.
			byte [] data = new byte[128];
			clientSocket.Receive(data);
			int length=BitConverter.ToInt32(data,0);

			clientSocket.Send(Encoding.ASCII.GetBytes(m_fileName+":"+"this is a test\r\n"));

			clientSocket.Send(Encoding.ASCII.GetBytes(m_fileName+":"+"THIS IS "));
			clientSocket.Send(Encoding.ASCII.GetBytes("ANOTHRER "));
			clientSocket.Send(Encoding.ASCII.GetBytes("TEST."));
			clientSocket.Send(Encoding.ASCII.GetBytes("\r\n"));
			clientSocket.Send(Encoding.ASCII.GetBytes(m_fileName+":"+"TEST.\r\n"+m_fileName+":"+"TEST AGAIN.\r\n"));
			clientSocket.Send(Encoding.ASCII.GetBytes("[EOF]\r\n"));

			// Get the total length
			clientSocket.Receive(data);
			length=BitConverter.ToInt32(data,0);
			clientSocket.Close();
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Fact Pandit
Web Developer
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 30 Dec 2003
Article Copyright 2003 by Fact Pandit
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid