473,395 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

TCP File Transfer application!

Hi all!

I'm trying to write a file transfer application using TCP, but I'm
having some problem to connect the client and the server.

Sniffing with Ethereal I can see the packets sent from the client:
SOURCE = 130.237.15.219 DESTINATION = 130.237.15.224 PROTOCOL = TCP;
INFO = 1464 > 11115 [SYN] Seq=0 Ack=0 Win=32768 Len=0 MSS=1460, but
there are no packets to reply, and after a while the client program
exit with
"Socket exception: A connection attempt failed because the connected
party did not properly respond after a period of time, or established
conection failed because connected host has failed to respond".

I'm trying to establish a connection and send a message following the
examples I have found in the msdn library,
http://msdn.microsoft.com/library/de...ctorTopic4.asp
http://msdn.microsoft.com/library/de...lienttopic.asp
Am I missing something??
Thanks a lot for any suggestion!!

Alessandro Sacchi
sa***************@gmail.com
Here there is the CLIENT code, trying to send a message to the server:

public void Connect()
{
try
{
Int32 port = 11115;
TcpClient client = new TcpClient(txtIPaddr.Text, port);
// ipAddr is a string containing the ip address of the server

string message = ("IS ANYBODY THERE?");
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

NetworkStream stream = client.GetStream();

stream.Write(data, 0, data.Length);
// Receive the response

// Buffer to store the response
data = new Byte[256];

// String to store the response ASCII representation
String responseData = String.Empty;

Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

txtConsole.Text = (responseData);

client.Close();
}
catch(ArgumentNullException e)
{
MessageBox.Show("Argument null exception: " + e.Message);
}
catch(SocketException ee)
{
MessageBox.Show("Socket exception: " + ee.Message);
}
}
And here there is the SERVER code:

public void StartServer()
{
try
{
IPAddress myIP = IPAddress.Parse("127.0.0.1");
Int32 port = 11115;

// Set up a TCP Listener
TcpListener server = new TcpListener(myIP, port);

// Start listening for client request
server.Start();

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;

while(true)
{
// Perform a blocking call to accept request
TcpClient client = server.AcceptTcpClient();

txtConsole.Text = "Client connected";

data = null;

// Get a stream for reading and writing
NetworkStream stream = client.GetStream();

int i;

// loop to receive all the data sent by the client
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

txtConsole.Text = (String.Format("Received: {0}", data));

byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

// Send back a response
stream.Write(msg, 0, msg.Length);

txtConsole.Text = (String.Format("Sent: {0}", data));
}

// Shutdown and close connection
client.Close();
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
Nov 17 '05 #1
3 8696
I don't know the tcpclient control but does the constructor actually open
the port? I see you call close() but not Connect()
(didn't look at help files, Index page is taking the usual 5 mins to update
itself)
Nov 17 '05 #2
Hi,

Use TcpClient client = new TcpClient( IpAddress.Parse( txtIPaddr.Text) ,
port);

IIRC the constructor TcpClient( hostname, port) try to resolve hostname.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Alessandro" <sa***************@gmail.com> wrote in message
news:3b**************************@posting.google.c om...
Hi all!

I'm trying to write a file transfer application using TCP, but I'm
having some problem to connect the client and the server.

Sniffing with Ethereal I can see the packets sent from the client:
SOURCE = 130.237.15.219 DESTINATION = 130.237.15.224 PROTOCOL = TCP;
INFO = 1464 > 11115 [SYN] Seq=0 Ack=0 Win=32768 Len=0 MSS=1460, but
there are no packets to reply, and after a while the client program
exit with
"Socket exception: A connection attempt failed because the connected
party did not properly respond after a period of time, or established
conection failed because connected host has failed to respond".

I'm trying to establish a connection and send a message following the
examples I have found in the msdn library,
http://msdn.microsoft.com/library/de...ctorTopic4.asp
http://msdn.microsoft.com/library/de...lienttopic.asp
Am I missing something??
Thanks a lot for any suggestion!!

Alessandro Sacchi
sa***************@gmail.com
Here there is the CLIENT code, trying to send a message to the server:

public void Connect()
{
try
{
Int32 port = 11115;
TcpClient client = new TcpClient(txtIPaddr.Text, port);
// ipAddr is a string containing the ip address of the server

string message = ("IS ANYBODY THERE?");
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

NetworkStream stream = client.GetStream();

stream.Write(data, 0, data.Length);
// Receive the response

// Buffer to store the response
data = new Byte[256];

// String to store the response ASCII representation
String responseData = String.Empty;

Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

txtConsole.Text = (responseData);

client.Close();
}
catch(ArgumentNullException e)
{
MessageBox.Show("Argument null exception: " + e.Message);
}
catch(SocketException ee)
{
MessageBox.Show("Socket exception: " + ee.Message);
}
}
And here there is the SERVER code:

public void StartServer()
{
try
{
IPAddress myIP = IPAddress.Parse("127.0.0.1");
Int32 port = 11115;

// Set up a TCP Listener
TcpListener server = new TcpListener(myIP, port);

// Start listening for client request
server.Start();

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;

while(true)
{
// Perform a blocking call to accept request
TcpClient client = server.AcceptTcpClient();

txtConsole.Text = "Client connected";

data = null;

// Get a stream for reading and writing
NetworkStream stream = client.GetStream();

int i;

// loop to receive all the data sent by the client
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

txtConsole.Text = (String.Format("Received: {0}", data));

byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

// Send back a response
stream.Write(msg, 0, msg.Length);

txtConsole.Text = (String.Format("Sent: {0}", data));
}

// Shutdown and close connection
client.Close();
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}

Nov 17 '05 #3
Hi Alessandro,

Sorry but the example before:
TcpClient( IPAddress.Parse(...), port ) does not compile !!!

It's connect the one who does support that call,

instead use this code:

TcpClient client= new TcpClient()

if ( Regex.IsMatch( txtIPaddr.Text, "\d+\.\d+\.\d+\.\d+") )
client.Connect( IpAddress.Parse( txtIPaddr.Text , port);
else
client.Connect( txtIPaddr.Text , port);
sorry for the confusion
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Alessandro" <sa***************@gmail.com> wrote in message
news:3b**************************@posting.google.c om...
Hi all!

I'm trying to write a file transfer application using TCP, but I'm
having some problem to connect the client and the server.

Sniffing with Ethereal I can see the packets sent from the client:
SOURCE = 130.237.15.219 DESTINATION = 130.237.15.224 PROTOCOL = TCP;
INFO = 1464 > 11115 [SYN] Seq=0 Ack=0 Win=32768 Len=0 MSS=1460, but
there are no packets to reply, and after a while the client program
exit with
"Socket exception: A connection attempt failed because the connected
party did not properly respond after a period of time, or established
conection failed because connected host has failed to respond".

I'm trying to establish a connection and send a message following the
examples I have found in the msdn library,
http://msdn.microsoft.com/library/de...ctorTopic4.asp
http://msdn.microsoft.com/library/de...lienttopic.asp
Am I missing something??
Thanks a lot for any suggestion!!

Alessandro Sacchi
sa***************@gmail.com
Here there is the CLIENT code, trying to send a message to the server:

public void Connect()
{
try
{
Int32 port = 11115;
TcpClient client = new TcpClient(txtIPaddr.Text, port);
// ipAddr is a string containing the ip address of the server

string message = ("IS ANYBODY THERE?");
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

NetworkStream stream = client.GetStream();

stream.Write(data, 0, data.Length);
// Receive the response

// Buffer to store the response
data = new Byte[256];

// String to store the response ASCII representation
String responseData = String.Empty;

Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

txtConsole.Text = (responseData);

client.Close();
}
catch(ArgumentNullException e)
{
MessageBox.Show("Argument null exception: " + e.Message);
}
catch(SocketException ee)
{
MessageBox.Show("Socket exception: " + ee.Message);
}
}
And here there is the SERVER code:

public void StartServer()
{
try
{
IPAddress myIP = IPAddress.Parse("127.0.0.1");
Int32 port = 11115;

// Set up a TCP Listener
TcpListener server = new TcpListener(myIP, port);

// Start listening for client request
server.Start();

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;

while(true)
{
// Perform a blocking call to accept request
TcpClient client = server.AcceptTcpClient();

txtConsole.Text = "Client connected";

data = null;

// Get a stream for reading and writing
NetworkStream stream = client.GetStream();

int i;

// loop to receive all the data sent by the client
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

txtConsole.Text = (String.Format("Received: {0}", data));

byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

// Send back a response
stream.Write(msg, 0, msg.Length);

txtConsole.Text = (String.Format("Sent: {0}", data));
}

// Shutdown and close connection
client.Close();
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Neel Word | last post by:
I need to develop an application (development platform: Win2000; target platform: Win98/ME/NT4/2000/XP), which would require file transfer functionality over dial-up and network. We could...
0
by: GrantS | last post by:
I need urgent help. Done OK for a newbie getting automation of Windows Messenger into a Winform C# app but have three problems that I urgently need pointers to. Point 1 and 2 are the same issue, I...
2
by: Landley | last post by:
Dear Fellow Coders, I am attempting to send files using UDP. I had no problem sending small files, but when the files were over a certain size an exception was thrown. It was moaning about the...
2
by: Bonzol | last post by:
vb.net 2003 Windows application We have a Client/Server system set up by communicating through a TCPClient object. The server loops continuously using a tcplistener device and each time a client...
10
by: David | last post by:
I have googled to no avail on getting specifically what I'm looking for. I have found plenty of full blown apps that implement some type of file transfer but what I'm specifcally looking for is an...
7
by: shadowman | last post by:
Hello all: I have a web app that creates an image of a graph (as a png), based on user input of a combination of drop-down box items. I'm trying to add a function that allows the user to save...
0
by: fiona | last post by:
Yucca Valley, CA, - October 2007: Catalyst Development Corporation, publisher of SocketTools, SocketWrench and LogicGem, today announced the release of Catalyst File Transfer .NET V5.0. For...
1
by: Saurabh | last post by:
Dear All, Can anyone tell me, how to write such a program that can transfer files (either binary or text) behind NAT devices( such as for computers behind firewalls and routers and other NAT...
2
by: kashifjawed | last post by:
I'm developing a c# asynchronous socket application for tranfering file or large data from client to server and server to client as well in chunks. Application sometimes transfer file from client...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.