C# network tutorial problem

Started by
2 comments, last by GameDev.net 17 years, 3 months ago
Yesterday i started with the network part in the book, "Microsoft Visual CSharp .NET 2003 Kick Start" , but when I was doing as in the book and started both files, first the server , then the program that was going to connect to my server program, it failed, i tryed it on 2 different computers, one with 2005 and one with 2003, both failed, anyway, here is the code: server code: using System.IO; using System.Net; using System.Net.Sockets; public class ch05_08 { public static void Main() { ch05_08 appObject = new ch05_08(); appObject.Listen(); } private void Listen() { TcpListener tcpListener = new TcpListener(IPAddress.Any, 65512); tcpListener.Start(); System.Console.WriteLine("Listening..."); while(true) { Socket socket = tcpListener.AcceptSocket(); if (socket.Connected) { System.Console.WriteLine("Connected..."); NetworkStream networkStream = new NetworkStream(socket); StreamWriter output = new StreamWriter(networkStream); StreamReader input = new StreamReader("ch05_08.cs"); string inputString; System.Console.WriteLine("Sending..."); while((inputString = input.ReadLine()) != null) { output.WriteLine(inputString); output.Flush(); } networkStream.Close(); input.Close(); output.Close(); socket.Close(); System.Console.WriteLine("Disconnected..."); break; } System.Console.WriteLine("Quitting..."); } } } and here is the client code: using System.Net.Sockets; public class ch05_09 { static public void Main() { TcpClient client = new TcpClient("localHost", 65512); NetworkStream network = client.GetStream(); System.IO.StreamReader input = new System.IO.StreamReader(network); string outputString; while((outputString = input.ReadLine()) != null) { System.Console.WriteLine(outputString); } input.Close(); network.Close(); } } in the book they say it will be like this Listening... Connected... Sending... Disconnected... but for me it only gets Listening... Connected... before an error occures. can anyone fix the code and tell me what was wrong?
Advertisement
You should post WHAT the error is, else we have a hard time figuring it out.

Is it an execption? What kind of exception? What line does the debugger point at when the exception happens?
enum Bool { True, False, FileNotFound };
I suspect the line
StreamReader input = new StreamReader("ch05_08.cs");

Did you give the file a different name? But yeah, it's much easier to work out what's going on if you provide the full exception listing (and hopefully we can show you how to use them so you can debug things yourself too!).

Try using [ source ] ... [ /source ] tags around large code blocks, too.
I tried and it works!

Server code:
public class ch05_08
{
string bella;

public static void Main()
{
ch05_08 appObject = new ch05_08();
appObject.Listen();

}

private void Listen()
{

TcpListener tcpListener = new TcpListener(IPAddress.Any, 65512);
while(bella != "close")
{
tcpListener.Start();

bella = System.Convert.ToString(System.Console.ReadLine());

System.Console.WriteLine("Listening...");

while(true)
{
Socket socket = tcpListener.AcceptSocket();

if (socket.Connected)
{
NetworkStream networkStream = new NetworkStream(socket);
StreamWriter output = new StreamWriter(networkStream);
StreamReader input = new StreamReader("ch05_08.cs");
string inputString = input.ReadLine();

System.Console.WriteLine("Sending...");

output.WriteLine(bella);
output.Flush();

networkStream.Close();
input.Close();
output.Close();
socket.Close();

System.Console.WriteLine("Mandato");
System.Console.WriteLine(inputstring2);
break;
}
System.Console.WriteLine("Quitting...");
}
}
}
}

Client Code:
public class ch05_09
{
static string yo = "";
static string bella2;

static public void Main()
{
System.Console.WriteLine("Inserire l'ip a cui connettersi");
string ip = System.Convert.ToString(System.Console.ReadLine());
while(yo != "close")
{
TcpClient client = new TcpClient(ip, 65512);
NetworkStream network = client.GetStream();
System.IO.StreamReader input = new System.IO.StreamReader(network);
StreamWriter input2 = new StreamWriter(network);

string outputString = input.ReadLine();

System.Console.WriteLine(outputString);
yo = outputString;

input.Close();
network.Close();

bella2 = System.Convert.ToString(System.Console.ReadLine());
string inpustring2 = input.ReadLine();
input2.WriteLine(bella2);
input2.Flush();
}
}
}

with this code i can write from the server to the client... like a chat(but only from server to client)... how can i do to write from client to server too?
Thanks^^

This topic is closed to new replies.

Advertisement