Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my TCP Server Code:-




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace TCPServer
{
class Program
{
private static TcpListener ObjTcpListener;
private static Thread ObjThread;
private static int filno = 1;

static void Main(string[] args)
{

try
{
Console.Write("\n\n Enter the port number: ");
int port = int.Parse(Console.ReadLine());
IPGlobalProperties ObjIPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] ObjTcpConnectionInformation = ObjIPGlobalProperties.GetActiveTcpConnections();
bool IsAvaliable = true;
foreach (TcpConnectionInformation tcpconection in ObjTcpConnectionInformation)
{
if (tcpconection.LocalEndPoint.Port == port)
{
IsAvaliable = false;

}

}
if (IsAvaliable == true)
{

ObjTcpListener = new TcpListener(GetIPAddress(), port);
ObjTcpListener.Start();
Thread.Sleep(2000);
Console.Write("\n\n Server Strated at IP: {0}", GetIPAddress());
Thread.Sleep(2000);
Console.Write("\n\n Listening Sucessfully On Port {0}", port);

ObjThread = new Thread(ListnerThread);
ObjThread.IsBackground = true;
ObjThread.Start();


}
else
{
Console.Write("\n\n Sorry port specified is not avaliable at the moment");

}



Console.ReadKey();
}
catch (Exception ex)
{


}

}
public static IPAddress GetIPAddress()
{
IPHostEntry hostEntry = Dns.GetHostEntry(Environment.MachineName);

foreach (IPAddress address in hostEntry.AddressList)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
return address;
}

return null;
}

public static void ListnerThread()
{

while (true)
{
TcpClient ObjRecievedTcpClient = ObjTcpListener.AcceptTcpClient();
try
{
if (ObjRecievedTcpClient.Connected == true)
{

Console.Write("\n\n File {0} is recieved sucessfully", filno);
string DestinationFilePath = string.Format("D:\\InBoundRecivedFiles\\file{0}", filno.ToString());
NetworkStream ObjStream = ObjRecievedTcpClient.GetStream();
byte[] ReciveingBuffer = new byte[ObjRecievedTcpClient.Available];
ObjStream.Read(ReciveingBuffer, 0, (int)ReciveingBuffer.Length);
FileStream readFile = new FileStream(DestinationFilePath, FileMode.Create, System.IO.FileAccess.Write);
readFile.Write(ReciveingBuffer, 0, ReciveingBuffer.Length);
readFile.Close();
ObjRecievedTcpClient.Close();
filno++;

}
}
catch (Exception ex)
{
Console.Write("\n\n Unable to Connect to the remote client\n\n Error: {0}", ex.Message.ToString());

}
}



}
}
}
Actually the problem is that when Local Client is requesting for files on the particular port it is getting all files but when remote client request for file read it is getting only 1 file.For example If port has 5 files then local client is getting all 5 files but remote client is getting only 1 file.Kindly tell me what is the issue
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900