first of all, hi new to the forums looking for some help (asked at other places too but they didnt seem to know)
ok the problem i have is i have downloaded an open source C# project (called yoozy.net - http://yoozy.sourceforge.net/) which allows you to access the files on a server running yoozy
i want to change it so that instead of the server application listening for the client, it does it the opposite way round so the 'server' app connects to the client this way i will be able to use it to access my files at home when im at school or at my dads.
i have some c# knowledge however not in sockets and stuff and i have tried to learn and made a chat application but the two different apps from yoozy use different things to connect to each other (netstream.cs are different) so i cant just swap stream.connect("127.0.0.1",123) for stream.listen();
if someone could have a look and help me (and tell me what they did) it will be appreciated
thankyou
people here are not robots scanning the forum every few minutes they're human beings and volunteers and answering questions is not obligatory so be patient and maybe someone will help you
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
You are unlikely to find someone who will read an entire project and explain it to you. If you don't know what you are doing there are resources all over the net that can help you. If you then have a *specific* question come back here and we may be able to help.
@ biged hey i think you misunderstood me, im not looking for someone to read through the project and explain it to me, im quite competent in C# myself.
however it seems i have hit a brick wall with this thing and there is a small section of code (mainly netcl.cs) which im asking an experienced c# programmer to look at and give me ideas or help me to achieve my goal. this shouldnt be too hard for a programmer who knows a fair bit about sockets and things. thanks anyways
@rliq thanks for the reply and i thought i have been specific . is there anything else you need to know?
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Network
{
public delegate void _OnConnect();
public delegate void _OnAccept(string name);
public delegate void _OnRecieveData(byte[] data);
public delegate void _OnSendData(int bytes);
public delegate void _OnCloseConnection();
public class NetStream
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Socket accepted = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint local = null;
int port = 28137;
public string delimiter = "netcl_end";
public string file_delimiter = "netcl_end_file";
byte[] signature;
byte[] buffer = new byte[1024];
public event _OnConnect OnConnect;
public event _OnAccept OnAccept;
public event _OnCloseConnection OnCloseConnection;
StringBuilder builder = new StringBuilder();
public NetStream()
{
signature = Encoding.Default.GetBytes(file_delimiter);
}
public NetStream(int port)
{
local = new IPEndPoint(IPAddress.Any, port);
signature = Encoding.Default.GetBytes(file_delimiter);
}
~NetStream()
{
if (socket.Connected)
{
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
if (accepted.Connected)
{
accepted.Shutdown(SocketShutdown.Both);
accepted.Close();
}
}
/// <summary>
/// Get or sets local port
/// </summary>
public int Port
{
set
{
port = value;
local = new IPEndPoint(IPAddress.Any, port);
}
get
{
return port;
}
}
public void Connect(string address, int port)
{
accepted = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress addr = IPAddress.Parse(address);
accepted.BeginConnect(addr, port, new AsyncCallback(ConnectCallback), null);
}
public void Shutdown()
{
if (socket.Connected)
{
socket.Shutdown(SocketShutdown.Both);
}
if (accepted.Connected)
{
accepted.Shutdown(SocketShutdown.Both);
}
socket.Close();
accepted.Close();
}
private void ConnectCallback(IAsyncResult ir)
{
try
{
accepted.EndConnect(ir);
if (OnConnect != null)
{
OnConnect();
}
}
catch (Exception)
{
OnCloseConnection();
}
}
public void AsyncListen()
{
socket.Bind(local);
socket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
public void Listen()
{
socket.Bind(local);
socket.Listen(1);
accepted = socket.Accept();
}
private void AcceptCallback(IAsyncResult ir)
{
accepted = socket.EndAccept(ir);
if (accepted != null)
{
if (OnAccept != null)
{
OnAccept(accepted.LocalEndPoint.ToString());
}
}
}
public byte[] Recieve()
{
//YoozyServer.FileLogger.Write("Recieve::");
StringBuilder str = new StringBuilder();
int r = accepted.Receive(buffer, 1024, SocketFlags.None);
string data = Encoding.Default.GetString(buffer, 0, r);
str.Append(data);
if (str.ToString().EndsWith(delimiter))
{
string recv = str.ToString().Replace(delimiter, "");
//YoozyServer.FileLogger.Write("Recieve::recieved: " + recv);
byte[] byte_recv = Encoding.Default.GetBytes(recv);
//byte[] decompressed = compress.DecompressData(byte_recv);
return byte_recv;
}
else
{
do
{
r = accepted.Receive(buffer, 1024, SocketFlags.None);
if (r == 0)
{
throw new SocketException();
}
data = Encoding.Default.GetString(buffer, 0, r);
str.Append(data);
}
while (!str.ToString().EndsWith(delimiter));
//prepare
string recv = str.ToString().Replace(delimiter, "");
byte[] byte_recv = Encoding.Default.GetBytes(recv);
//byte[] decompressed = compress.DecompressData(byte_recv);
return byte_recv;
}
}
bool IsFileEndSignature(byte[] source, byte[] sign)
{
if (source.Length == sign.Length)
{
for (int i = 0; i < sign.Length; i++)
{
if (source[i] != sign[i])
{
return false;
}
}
}
else
{
return false;
}
return true;
}
public void Send(byte[] data)
{
accepted.Send(data);
}
}
}
this is how it is when its downloaded. the client has listen() in it for some reason but its asynchronous i swapped it for the servers listen method and the server connect method for the client connect and it does kinda work, although if i press "listen" on client it then hangs until i start the server and then carrys on working as normal but without a connection and the server crashs
its easier to understand if you looked at the source
Bookmarks