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);
}
}