I want to keep the database Files only on the server so when i upload a file i want to send it to "127.0.0.1" i tried the following code
private void SendFile(string fileName = "C:\\test.txt")
{
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);
Socket client = new Socket(ipAddr.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipEndPoint);
byte[] fileBytes = File.ReadAllBytes(fileName);
client.Send(fileBytes);
client.Shutdown(SocketShutdown.Both);
client.Close();
}
But it keep getting me this Error : "No connection could be made because the target machine actively refused it " Which Means Server is not listening.
So,
1) How fix this in order to be able to send any file to localhost
2) How i can open files in the browser via "127.0.0.1[FileName].[Ext]"
You need to write something to listen to the port using the SocketServer class. The code you wrote expects a server to be listening to a connection in the 11000. This is done using the Listener.Listen() method. A full example: http://msdn.microsoft.com/en-us/library/fx6588te(v=vs.110).aspx
Related
I'm trying to get my client app to connect to a server on a remote machine operating on my local network without knowing the server's IP Address to begin with.
Ideally I'd like the client to connect automatically, but since I've been having trouble getting that to work properly, I thought for now I'd have the user manually input their server's IP Address into the client.
I've saved the inputted IP address to a file and loaded it into a string:
string ServerIP = System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + #"/ServerIP.cfg");
but I'm not sure how to set that string as the IPEndPoint:
private IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("Insert ServerIP string here"), 8888);
I've unsuccessfully tried using ipString, IPAddress.Parse(ServerIP, 8888);, IPAddress.Parse(string ServerIP, 8888); and multiple other combinations, I still can't figure out the proper syntax for this method, and my Google-fu has failed me on this one.
EDIT: With this implementation I don't get any errors until I try to debug, and I get:
"An unhandled exception of type 'System.ArgumentNullException' occurred in System.dll
Additional information: Value cannot be null." on Client.Connect(serverEndPoint);
private IPEndPoint serverEndPoint;
private TcpClient Client = new TcpClient();
public Client()
{
InitializeComponent();
Client.Connect(serverEndPoint);
string ServerIP = System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + #"/ServerIP.cfg");
serverEndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), 8888);
}
IPAddress.Parse only takes one argument, I believe you meant to put the port in the IPEndPoint constructor and not the Parse method.
private IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), 8888);
Server :
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(IPAddress.Parse("90.181.x.xxx"), 23466));
socket.Listen(1);
Socket accepteddata = socket.Accept();
data = new byte[accepteddata.SendBufferSize]; 6
int j = accepteddata.Receive(data);
byte[] adata = new byte[j];
for (int i = 0; i < j; i++)
adata[i] = data[i];
string dat = Encoding.Default.GetString(adata);
MessageBox.Show(dat);
And client:
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
s.Connect(IPAddress.Parse("90.181.x.xxx"), 23466);
string q = "It work";
byte[] data = Encoding.Default.GetBytes(q);
s.Send(data);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
The problem is when my friend connect to it he gets error.Yes I have port forwarded.I have set up like this Internal/External port start/end to 23466 and ip address to 192.168.1.1
this line here:
socket.Bind(new IPEndPoint(IPAddress.Parse("90.181.x.xxx"), 23466));
should be:
socket.Bind(new IPEndPoint(IPAddress.Any, 23466));
Of course you can bind to a specific IP address but it shouldn't be hard coded. "IPAddress.Any" should bind the socket to all addresses on the local machine. The problem you are having is you can't bind to the address of another machine(router).
also, does this code even compile? what does the '6' do?
data = new byte[accepteddata.SendBufferSize]; 6
The code is weird anyhow. After running the server try opening the command prompt and type "telnet localhost 23466" see if it opens a connection. Your friend or you, doesn't matter can also do "telnet 90.181.x.x 23466"
Make sure you close your sockets when you're finished with them.
I am creating a Socket connection with C# client socket and Java Server Socket.
When i am sending data from client socket,the server socket is properly receiving that data.
But when i am trying to send data back to Client socket from Server socket it is getting hanged on client side in receiving data.
Client Side Code(In C#.net)
clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
string hostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(hostName);
System.Net.IPAddress[] ipAddresses = hostEntry.AddressList;
System.Net.IPEndPoint remoteEP =
new System.Net.IPEndPoint(ipAddresses[ipAddresses.Length - 1], port);
clientSocket.Connect(remoteEP);
string sendData = inputFilePath;
byte[] byteDataSend = System.Text.Encoding.ASCII.GetBytes(sendData);
clientSocket.Send(byteDataSend);
int receivedBufferSize = clientSocket.ReceiveBufferSize;
byte[] recivedData = new Byte[receivedBufferSize];
int receivedDataLength = clientSocket.Receive(recivedData);
string stringData = Encoding.ASCII.GetString(recivedData, 0, receivedDataLength);
textFilePath = stringData;
Console.Write(stringData);
clientSocket.Close();
Server Socket Code (In Java)
Socket connection = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
fileName = in.readLine();
convertedFile =runConverter.convertDocumet(fileName);
byte[] sendingData = convertedFile.getBytes("US-ASCII");
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
dos.write(sendingData, 0, sendingData.length);
Tell me what is problem??
Please help...
The usual problem with that kind of c# code is the synchronous receive.
I always recommend doing an asynchronous read, as in this answer.
I'm not positive that that's the source of your problem, but if you implemented the asynchronous receive with a bit of logging there's a good chance that that will either fix your problem or make it much more obvious as to what your problem is.
A hang on synchronous receive does suggest that the Java isn't sending data to the same socket that the c# is listening on, so double-checking those endpoints can also be a good idea.
Hope that helps!
I'm trying to create a sever application on PC for many android devices using the same wi-fi network.
The devices will find the server's IP by receiving UDP broadcast from it contains the server IP data.
I've started by creating a sample udp broadcaster in C# and udp receiver in java but I never managed to get the packet on the android side . here is the code :
C#:
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Broadcast, listenPort);
listener.Connect(groupEP);
listener.EnableBroadcast = true;
byte[] data = new byte[1024];
try
{
while (!done)
{
Console.WriteLine("broadcast");
Thread.Sleep(400);
listener.Send(data,2);
}
Android code :
DatagramSocket socket;
try {
socket = new DatagramSocket(11000);
socket.connect(getBroadcastAddress(), 11000);
socket.setBroadcast(true);
byte[] buf = new byte[4];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
The Internet Permission is set correctly in the manifest. still not able to receive the packets.
Suggestions:
Make sure you don't have any firewalls (software or hardware) blocking you
Consider using Wireshark:
http://www.wireshark.org/
Look at this example:
http://code.google.com/p/boxeeremote/wiki/AndroidUDP
How can i get a file from remote computer? i know remote computer ip and 51124 port is open. i need this algorith:(this is a Windows Application visual studio 2008)
1) Connect 192.xxx.x.xxx ip via 51124 port
2) filename:123456 (i want to search it on remote machine)
3) Get File
4) Save C:\
51124 port is open. can i access and can i search any file according to filename?
My code is below:
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 51124);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
sock.Listen(maxConnections);
Socket serverSocket = sock.Accept();
byte[] data = new byte[bufferSize];
int received = serverSocket.Receive(data);
int filenameLength = BitConverter.ToInt32(data, 0);
string filename = Encoding.ASCII.GetString(data, 4, filenameLength);
BinaryWriter bWrite = new BinaryWriter(File.Open(outPath + filename, FileMode.Create));
bWrite.Write(data, filenameLength + 4, received - filenameLength - 4);
int received2 = serverSocket.Receive(data);
while (received2 > 0) {
bWrite.Write(data, 0, received2);
received2 = serverSocket.Receive(data);
}
bWrite.Close();
serverSocket.Close();
sock.Close();
MyQuery(targetip, port, filename) i can use it like that: MyQuery(192.xxx.x.xxx,51124,"MyNeddedFile");
MyQuery(targetip, port, filename)
{
.....
...
..
.
}
You have been trying to ask this question a few times now - perhaps this explains why we cannot answer your question:
If you have an FTP server, it will (by default) listen on port 21. So if I send a message according to the FTP protocol to port 21, it will respond.
If I have apache or IIS (or some other webserver) listening on for instance port 80, and I send an FTP message to it, it will give me an error, because they are expecting HTTP requests.
Without knowing what application is listening on port 51124, we can't possibly tell you how to talk to it.