![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2006
Posts: 41
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
I think it just means that the server is not listening on the specified port
__________________
JG-Webdesign |
|
|
|
|
|
#3 |
|
Professional Programmer
|
Actually you should take a look at some tutorials http://www.google.ro/search?q=asynch...ient=firefox-a
The start again . Also, that while(true) inside for(;; ) ... is really something - you really want it to stay in that infinite loop ![]()
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#4 |
|
Professional Programmer
|
You might have made a mistake in the code responsible for getting the port number.
__________________
JG-Webdesign |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Socket Server | hbe02 | C++ | 19 | May 23rd, 2006 10:14 PM |
| FTP Server in Python | Sane | Python | 1 | Mar 31st, 2006 10:25 PM |
| socket tcp/ip | myName | C++ | 5 | Feb 22nd, 2006 9:53 AM |
| Instant Messaging App Help | AusTex | C | 0 | Apr 27th, 2005 4:52 PM |