Forum » Programiranje » c# tcp problem
c# tcp problem
rushhh ::
V posebej razredu imam nareto vse za server in client in streamanje. Zanima me zakaj mi debuger javi napako, namreč Networkstream javi napaka oz. je NULL..tukaj je del kode:
Networkstream imam globalno spremenljivko.
public void start_client()
{
IPAddress novip = IPAddress.Parse("127.0.0.1");
Console.WriteLine("Povezujem ...");
TcpClient odjemalec = new TcpClient();
odjemalec.Connect(novip, int.Parse("1111"));
NetworkStream networkStream = odjemalec.GetStream();
Console.WriteLine("Povezan");
/*Console.Write("Vpiši besedilo: ");
String line = Console.ReadLine();*/
}
public void streamaj_client()
{
StreamReader streamReader = new StreamReader(networkStream); //TLELE JE streamreader NULL
StreamWriter streamWriter = new StreamWriter(networkStream);
streamWriter.WriteLine(podatki);
Console.WriteLine("Pošiljam sporočilo...");
streamWriter.Flush();
string s = streamReader.ReadLine();
Console.WriteLine("Prejemam sporočilo...");
Console.WriteLine(s);
}
Networkstream imam globalno spremenljivko.
Keki ::
A je tudi networkStream null v tem trenutku?Oz mogoče ti javi da ga moreš inicializirat...kaj ti sploh javi debuger?
http://www.alfakan.si/
http://mihajerot.si/
http://mihajerot.si/
rushhh ::
Ja networkstream je isto nula..pač client se lahko poveže na server. potem ko stisnem v formi clienta na gumb pošlji serverjo, ta kliče to metodo streamaj_client...tlele je še koda za streamaj_server, ki jo kličem ko zaženem server..
Koda za start serverja:
Ne vm ka bi lahk blo narobe??
public void streamaj_server()
{
try
{
Socket socketForClient = server.AcceptSocket();
Console.WriteLine("Odjemalec povezan " + socketForClient.RemoteEndPoint.ToString());
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamWriter streamWriter = new StreamWriter(networkStream);
StreamReader streamReader = new StreamReader(networkStream);
podatki = streamReader.ReadLine();
Console.WriteLine("Sprejeto: " + podatki);
//textBox1.Text = line;
streamWriter.WriteLine(podatki);
Console.WriteLine("Poslano: " + podatki);
streamWriter.Flush();
Console.WriteLine();
socketForClient.Close();
}
catch (Exception x)
{
Console.WriteLine("Error..... " + x.StackTrace);
}
}
Koda za start serverja:
public void start_server()
{
ip = IPAddress.Parse("127.0.0.1");
server = new TcpListener(ip, int.Parse("1111"));
server.Start();
}
Ne vm ka bi lahk blo narobe??
win64 ::
tukaj prirediš lokalno spremenljivko in ne "globalno":
NetworkStream networkStream = odjemalec.GetStream();
Spremeni v to:
networkStream = odjemalec.GetStream();
NetworkStream networkStream = odjemalec.GetStream();
Spremeni v to:
networkStream = odjemalec.GetStream();
Zgodovina sprememb…
- spremenil: win64 ()
rushhh ::
Neki me zanima..recim da v server form load zaženem server in začnem streamanje preko socketov...kot je tlele
Zei pa za vsako streamanje, ko hočem poslat recim nek fajl iz clienta mi prvič pošlje drugič pa ne...tlele mam kodo za pošiljanje
Zei me zanima a morem za vsak podatek, ki ga pošljem zapreti streamwriter, streamreader in networkstream..ker mi namreč v obeh primerih v drugo ne dela?
private void server_Load(object sender, EventArgs e)
{
Int32 port = 1111;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
TcpListener myList = new TcpListener(localAddr, port);
myList.Start();
Console.WriteLine("Strežnik pognan");
try
{
Socket socketForClient = myList.AcceptSocket();
Console.WriteLine("Odjemalec povezan " + socketForClient.RemoteEndPoint.ToString());
NetworkStream networkStream = new NetworkStream(socketForClient);
StreamWriter streamWriter = new StreamWriter(networkStream);
StreamReader streamReader = new StreamReader(networkStream);
line = streamReader.ReadLine();
Console.WriteLine("Sprejeto: " + line);
textBox1.Text = line;
streamWriter.WriteLine(line);
Console.WriteLine("Poslano: " + line);
streamWriter.Flush();
Console.WriteLine();
}
catch (Exception x)
{
Console.WriteLine("Error..... " + x.StackTrace);
}
}
Zei pa za vsako streamanje, ko hočem poslat recim nek fajl iz clienta mi prvič pošlje drugič pa ne...tlele mam kodo za pošiljanje
private void poslji()
{
if (comboBox1.SelectedIndex == 0) novo = "1";
if (comboBox1.SelectedIndex == 1) novo="2";
NetworkStream networkStream = odjemalec.GetStream();
StreamReader streamReader = new StreamReader(networkStream);
StreamWriter streamWriter = new StreamWriter(networkStream);
streamWriter.WriteLine(novo);
Console.WriteLine("Pošiljam sporočilo...");
streamWriter.Flush();
string s = streamReader.ReadLine();
textBox1.Text = s;
Console.WriteLine("Prejemam sporočilo...");
Console.WriteLine(s);
}
Zei me zanima a morem za vsak podatek, ki ga pošljem zapreti streamwriter, streamreader in networkstream..ker mi namreč v obeh primerih v drugo ne dela?
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | Socket ServerOddelek: Programiranje | 2452 (1645) | fx |
» | [C#] Kopiranje osebe iz seznama v drug seznamOddelek: Programiranje | 1512 (1373) | Presidente |
» | C# (strani: 1 2 )Oddelek: Programiranje | 12163 (8998) | Ericssony |
» | [C#] Client -> Server komunikacija dela, kako je pa z Server -> Client?Oddelek: Programiranje | 1296 (1162) | Mitja Bonča |
» | [Visual C#] zaradi while zanke se ne pokaže oknoOddelek: Programiranje | 1009 (886) | Fresh-Grass |