| csrocker101 |
Mar 14th, 2007 3:24 PM |
Help with C# TCP/IP Server
Ok so Basically i've got a very simple C# server that recieves information from a client. My code for the Server is as follows:
:
using System;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Net;
namespace _8246_ACW_Coursework
{
class ServerApp
{
public void runServer()
{
//creates a TcpListener Object to listen for incoming connections
TcpListener listener;
//creates a socket to send and recieve data
Socket connection;
//creates a network stream
NetworkStream socketStream;
for (;;)
{
try
{
//creates an instance Tcp listenter to listen for connections on port 43
listener = new TcpListener(43);
//begins listening for incoming connections
listener.Start();
while (true)
{
//starts new thread
Thread thread = new Thread(runServer);
thread.Start();
//accepts socket if request is made to connect
connection = listener.AcceptSocket();
//sets a network stream instance to the connection socket
socketStream = new NetworkStream(connection);
//calls method doRequest which takes a socketstream and handles its request
doRequest(socketStream);
//closes the socket stream
socketStream.Close();
//closes connection
connection.Close();
}
}
catch (Exception e)
{
e.ToString();
}
}
}
public string doRequest(NetworkStream socketStream)
{
//streamReader reads incoming connection from client
StreamReader sr = new StreamReader(socketStream);
//writes in coming data
StreamWriter sw = new StreamWriter(socketStream);
//Assigns data from the streamreader to a string
string streamReader = sr.ReadLine();
return streamReader;
}
}
}
I have a client, which connects to the server.
:
using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace _8246_ACW_Coursework
{
public class ClientApp
{
//creates a new instance of the clientform windows form application
static ClientServerGUI clientform = new ClientServerGUI();
//variables
public static string address,
sendData,
updateData,
userName;
private static string space = " ";
public static int port;
//method which connects to the server and returns information back to the forms class
public static string SendData()
{
TcpClient client = new TcpClient();
//calls the TCP client and connects to the address and port passed in by the User from GUI
client.Connect(address, port);
//Creates a stream writer instance so user can send data to server
StreamWriter sw = new StreamWriter(client.GetStream());
//reads the information sent back from the server
StreamReader sr = new StreamReader(client.GetStream());
//if statement evalutes to see if the user has selected to update the server
//" " = update server
//"" = do not update the server
if(updateData.Equals(""))
{
space = "";
}
else if(!updateData.Equals(""))
{
space = " ";
}
//Refrences stream writer, username variable passed in from GUI
//space variable provides update function: "" = dont update. " " = update database.
sw.WriteLine(userName + space + updateData);
sw.Flush();
//data send back from the server assigned to string variable
string recieved = sr.ReadLine();
return recieved;
}
}
}
For some reason, when I run the server code by itself in a seperate project and as a console application, then run my client from its seperate project it works just fine and I am able to make contact with the server and recieve data. When I put it in the same project as my client application and make it as a generic C# class and attempt to export the data to a windows form, it cannot seem to access the server. I get the follow error message saying "No connection could be made because the target machine actively refused it." Which basically means that the client cannot find the host specified. Any suggestions of what to do? A few mates of mine said I have to do some multithreading but I am unsure how to do this. Any help or suggestions would be greatly appreciated.
|