I am writing FTP client with C#. I typed methods to upload file to FTP server and file upload does work. However, after successful data transmission, client gets disconnected from server. Here are steps I do:
1. Get IP and Port from server by using PASV.
2. Create DATA connection with server using IP and port.
3. Convert file to bytes and send through DATA connection.
4. Send STOR through COMMAND connection
My question is why I get disconnected.
public void PrepareUpload() // Get IP and Port from server by using PASV.
{
String answer;
String message = "PASV\r\n";
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
this.ns.Write(data, 0, data.Length);
answer = Response(this.ns);
this.dataPort = getPort(answer, 4) * 256 + getPort(answer, 5);
}
public void DataConnect(string server) // Create DATA connection with server using IP and port.
{
int port = this.dataPort;
this.dataConnection = new TcpClient();
IPAddress ipAddress = Dns.GetHostEntry(server).AddressList[0];
this.dataConnection.Connect(ipAddress, port);
this.nds = dataConnection.GetStream();
}
public void DataTransfer(string filename) // Convert file to bytes and send through DATA connection.
{
byte[] data = System.IO.File.ReadAllBytes(filename);
this.filename = Path.GetFileName(filename);
nds.Write(data, 0, data.Length);
}
public void Upload() // Send STOR through COMMAND connection
{
String message = "STOR " + this.filename + "\r\n";
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
this.ns.Write(data, 0, data.Length);
}
The order you describe is wrong. In particular you should not start data transfer before issuing the command which specifies what should happen with the transferred data (i.e. STOR). The correct order would be:
Use PASV or PORT command to determine data port and get response to this command. In case of PORT listen on the given IP:port.
Send the STOR command and read the response. It should be a preliminary response (150).
Create the data connection: with PASV connect to the remote host, with PORT wait for an incoming connections.
Transfer the data and close the data connection.
Wait for the final response (226).
Related
I have a client server model.
192.168.77.6 is the client (my code). 192.168.77.18 is a server device. I am sending a request to the server for a response. Once I get the response, I am reading the response and trying to send an acknowledgement of 2 bytes (line 479 of wireshark packet). But, before I can send my acknowledgement, I see that an acknowledgement of 0 bytes is being sent from the client (line 429 of wireshark packet), which causes the server device to send a [FIN,ACK] packet (line 475 of wireshark packet), and it doesn't accept the acknowledgement that I am sending (line 479 of wireshark packet).
Below is the screenshot of the wireshark packets obtained:
Wireshark packet screenshot
I have included my code below:
TcpClient tcpclnt;
NetworkStream stream;
tcpclnt = new TcpClient();
tcpclnt.Connect(ip, port);
stream = tcpclnt.GetStream();
if (tcpclnt.Connected)
{
SendMessage(stream, RequestToSend);
Thread.Sleep(2000);
string readData = "";
readData = readBytes1(stream);
}
tcpclnt.Close();
Below is the readBytes1() function:
private static string readBytes1(NetworkStream stream)
{
byte[] resp = new byte[100];
var memoryStream = new MemoryStream();
int bytes;
do
{
bytes = 0;
bytes = stream.Read(resp, 0, resp.Length);
memoryStream.Write(resp, 0, bytes);
}
while (bytes > 0);
GenerateAcknowledgement(stream);
return Encoding.GetEncoding("ISO-8859-1").GetString(memoryStream.ToArray());
}
GenerateAcknowledgement() is the function that sends 2 bytes of data.
You need to obtain the server and protocol.
The server is closing the connection. This will be in the design of the server.
It's possible that you're not doing something in the request to the server which causes it to close immediately.
Without knowing the protocol or having the server code it's not possible to know why the the server is closing the connection.
I'm trying to communicate with a device that has a Serial COM port that goes out (using RS232 Protocol), which I've hooked up to a converter box that converts the connection to Ethernet.
I've successfully connected to the converter box (which has its own IP) and I've successfully communicated with the device by sending it commands over telnet by using PuTTY, to which it has responded with various data that I've been able to parse.
I've established a connection by creating a new TcpClient, and I'm able to send strings to the device, but every time I get a response back, it's always "??\u0003" which I've researched and found that \uhhhh is a Unicode escape protocol. This confuses me because I have used ASCII encoding for everything.
public string TcpConnect(string cmd)
{
var client = new TcpClient();
//cmd = "ping";
Console.WriteLine("Connecting to server");
client.Connect("169.254.20.40", 23); //22 = ssh, 23 = telnet, 80 = http
Console.WriteLine("CONNECTED SUCCESSFULLY");
Stream tcpStream = client.GetStream();
ASCIIEncoding A = new ASCIIEncoding();
byte[] enable = A.GetBytes("enable" + Environment.NewLine);
byte[] connect = A.GetBytes("connect line 1" + Environment.NewLine);
byte[] ba = A.GetBytes(cmd);
Console.WriteLine("Transmitting.....");
tcpStream.Write(enable, 0, enable.Length);
tcpStream.Write(connect, 0, connect.Length);
tcpStream.Write(ba, 0, ba.Length);
byte[] responseBytes = new byte[4096];
int numBytesRead = tcpStream.Read(responseBytes, 0, responseBytes.Length);
var message = A.GetString(responseBytes, 0, numBytesRead);
Console.WriteLine("\nServer Closed.");
return message;
}
If I were to pass in "$TEAA4B9\r\n" as the message, I would expect something along the lines of "$TEA,086,040,031,000,3798" which is nowhere close to what I'm getting (which is ??\u0003)
Nevermind figured it out
Just kidding! Here's what I did: I added a "System.Threading.Thread.Sleep("1000") before the "tcpStream.Read" line at the bottom, and now it outputs the data I need. The device was outputting garbage on the first line (perhaps a handshake, not sure) and that's all that was being read before it was being stored into "message" and returned (not enough time was spent reading before it moved on to the next line of code)
So, I am working on a Game that uses a Server. I am trying to add a username & password system, but have run into a issue. When I send the Data from the client to the server, the Server receives it, but I can't think of a system to specify what the data is and where is should be stored.
For example, say I send the Username from my Client to the Server. The Server receives it, but how can I let it know that it's a username, not a password?
If anyone can help, thank you.
Note: If it helps, I am using C# in Visual Studio.
Server Code
int recv; // Holds how much data we are reading
byte[] data = new byte[1024]; // Byte Array of data, used for everything recived and sent to the server
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 904); // Listener for connections
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // Stores connection from client
socket.Bind(endpoint); // Bind incoming connection to socket
Console.WriteLine(" >> Waiting for client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 904); // Wait for connection, once recived, sets sender to connected IP
EndPoint tmpRemote = (EndPoint)sender; // Stores client temp
recv = socket.ReceiveFrom(data, ref tmpRemote); // Stores all client info, get data it's sending (And how much data is in it)
Console.WriteLine(" >> Data received from {0}", tmpRemote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); // Convert data into string and display it
string welcome = " >> Welcome to AlphaNET!"; // Welcome Message
data = Encoding.ASCII.GetBytes(welcome); // Convert Welcome to sendable bytes
if (socket.Connected) //Checking if socket is still connected, if so, send welcome message
{
socket.Send(data); // Send welcome message
}
while (true) // Our loop to check for data, goes on forever
{
if (!socket.Connected) // Checks to see if client is not connected, if true, write to console and break the loop
{
Console.WriteLine(" >> Data Sent");
}
data = new byte[1024]; // Resets data var
recv = socket.ReceiveFrom(data, ref tmpRemote);
if (recv == 0) // If no info is recived, break loop
{
break;
}
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
}
Console.ReadLine();
socket.Close();
Client Code
public void sendUsr()
{
String username = usrBox.Text;
byte[] packetData = System.Text.ASCIIEncoding.ASCII.GetBytes(username);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); // Makes socket to send data
socket.SendTo(packetData, ep); // Sends data to server
}
As rob said, you may want to format your data using data-interchange format, before you send them. I prefer use JSON provided by Newtonsoft.Json libary. You need to have a reference to it.
Client Code
String dataToSend = JsonConvert.SerializeObject(new { username = usrBox.Text, password = pwdBox.Text });
Server Code
recv = socket.ReceiveFrom(data, ref tmpRemote); // Stores all client info, get data it's sending (And how much data is in it)
dynamic receivedData = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(data, 0, recv));
String username = (string)receivedData.username;
String password = (string)receivedData.password;
I have to develop a simple TCP File Transfer Client as part of an exercise/demonstration but I am having trouble with data delivery.
I am already provided with the TCP File server which will receive the incoming files and I have to code a Client that will connect to and send a file to the server. Thus far I have been successful as far as converting the data of the selected file into to a format ready to transferring and successfully opening a connection and then sending of the data, however even encounter a problem on server that is receiving - I am not allowed to change the code of the server and should thus change my client code in such a way that the data sent can be interpreted by the server. Here is the code I use (some is left out which is simple overhead, like input-box to get IP address etc.):
Link for TCP Server VS Solution (if you prefer): https://www.mediafire.com/?682owf9wtdzmxac
TCP File Transfer Client Sending method:
private void TransferFile(string _sFileName, string _sIPAdress)
{
//Convert data for transfer
Stream strmfilestream = File.OpenRead(_sFileName);
Byte[] bFileBuffer = new Byte[strmfilestream.Length];
//Open TCP/IP Connection
TcpClient tcpClientSocket = new TcpClient(_sIPAdress,8080);
NetworkStream nsNetworkStream = tcpClientSocket.GetStream();
nsNetworkStream.Write(bFileBuffer,0,bFileBuffer.GetLength(0));
nsNetworkStream.Close();
}
*Note: _sFileName is just the full file path + File name from a OpenFileDialog.
Here is the Load Method of the server to get things going:
if (!Directory.Exists(#"C:\TCPFileServer"))
Directory.CreateDirectory(#"C:\TCPFileServer");
//Get Ip address of server host machine
IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName());
lblServerIP.Text = IPHost.AddressList[5].ToString();
lstSockets = new ArrayList();
Thread thdListener = new Thread(new ThreadStart(listenerThread));
thdListener.IsBackground = true; //This will enabe the thread to terminate when application is closed
thdListener.Start();
Here is the listener thread method:
public void listenerThread()
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, 8080);
tcpListener.Start();
while (true)
{
Socket handlerSocket = tcpListener.AcceptSocket();
if (handlerSocket.Connected)
{
this.Invoke((Action)(() => lstConnections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + " connected.")));
lock (this)
{
lstSockets.Add(handlerSocket);
}
ThreadStart thdsHandler = new ThreadStart(handlerThread);
Thread thdHandler = new Thread(thdsHandler);
thdHandler.Start();
}
}
}
And then lasty here is the handler thread method:
public void handlerThread()
{
try
{
int iBlockSize = 1024 * 3000; //3mb block size
Byte[] dataByte = new Byte[iBlockSize];
Byte[] rcvdData = new Byte[128000 * 1024];//128mb File Limit
Socket handlerSocket = (Socket)lstSockets[lstSockets.Count - 1];
NetworkStream networkStream = new NetworkStream(handlerSocket);
int i = 0;
int iRcvdBytes = 0;
while (true)
{
//Read from socket and store to buffer 'dataByte'
iRcvdBytes = networkStream.Read(dataByte, 0, iBlockSize);
dataByte.CopyTo(rcvdData, i);//Copy recieved bytes,from buffer, to another byte array
i += iRcvdBytes;
if (iRcvdBytes == 0) break;
}
//Get the File name length, BitConvertor occupies the first 4 bytes
int iFileNameLength = BitConverter.ToInt32(rcvdData, 0);
//Get the file name using length as the size and 4 as the offset
string sFileName = Encoding.ASCII.GetString(rcvdData, 4, iFileNameLength);
Stream fileStream = File.Open("C:\\TCPFileServer\\" + sFileName, FileMode.Create);
//Populate raw File on local machine
fileStream.Write(rcvdData, 4 + iFileNameLength, i - 4 - iFileNameLength);
fileStream.Close();
//Update BRS Net Files Server Log
this.Invoke((Action)(() => lstConnections.Items.Add(sFileName + ": Transfered.")));
//Close Connection
networkStream.Close();
handlerSocket = null; //Clear socket
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Now I have debugged and as far I can determine, the first time I can see a problem is when we are trying to determine the file name length where the code reads int iFileNameLength = BitConverter.ToInt32(rcvdData, 0); - When debugging this variable is always determined as '0', which I assume is not correct? I am not sure. The required results that needs to be achieved is that the Server should receive the file and after the successful transfer, the file name should be displayed in the ListBox.
Here is screen shots showing the problem i'm experiencing:
We can see that bytes are definitely received:
Note that the file name can not be retrieved from using the file name length and offset:
And thus, the following error occurs:
My experience and expertise lie elsewhere and this is the first time I am coding within this paradigm(File transfer over network). I do however have theoretical knowledge about/studied the OSI model and TCP/IP Protocol stack, but never coded anything like this. One note given to me was that the server was coded with the assumption that it will be executed on a specific PC and I am allowed to change code of the Server application if it is absolutely necessary.
Try this. You have to do thinks in reverse on the client and using a List makes things easier.
private void TransferFile(string _sFileName, string _sIPAdress)
{
List<Byte> bFileBuffer = File.ReadAllBytes(_sFileName).ToList();
byte[] bFileName = Encoding.ASCII.GetBytes(_sFileName);
bFileBuffer.InsertRange(0, bFileName);
//Get the File name length, BitConvertor occupies the first 4 bytes
byte[] brcvdDataCount = BitConverter.GetBytes((UInt32)_sFileName.Count());
bFileBuffer.InsertRange(0, brcvdDataCount);
//Open TCP/IP Connection
TcpClient tcpClientSocket = new TcpClient(_sIPAdress, 8080);
NetworkStream nsNetworkStream = tcpClientSocket.GetStream();
nsNetworkStream.Write(bFileBuffer.ToArray(), 0, bFileBuffer.Count);
nsNetworkStream.Close();
}
There are two things with the server code you should be aware of
/Get the File name length, BitConvertor occupies the first 4 bytes
int iFileNameLength = BitConverter.ToInt32(rcvdData, 0);
//Get the file name using length as the size and 4 as the offset
string sFileName = Encoding.ASCII.GetString(rcvdData, 4, iFileNameLength);
1) You need to add a byte count to beginning of upload.
2) The code is using Ascii Encoding which means you can not upload binary data.
I'm trying to learn the basics of networking and I've built an echo server from this tutorial. I checked the server with telnet and it works perfect.
Now when I'm using some of the many client samples on the Internet:
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
It doesn't work very well. If I will comment the stream.Read line, everything works perfect (expect I can't read). I was also trying to accomplish that in a similar way using asynchronous callback method for the read. and then it only works after I terminate the program (the server handles the request)
I suspect that the way I'm reading from the stream cause this block, but I'm too clueless to understand what I'm doing wrong.
The implementation will block until at least one byte of data can be
read, in the event that no data is available.
From MSDN
Your server propably isn't sending you any data.
Edit:
I tested your client and it works perfectly fine. Try it yourself and set the following parameters:
string server = "google.com";
int port = 80;
string message = "GET /\n";
It's definitely your server which has the problem.