Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2007, 2:24 PM   #1
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 41
Rep Power: 0 csrocker101 is on a distinguished road
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.
csrocker101 is offline   Reply With Quote
Old Mar 14th, 2007, 5:32 PM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 416
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
I think it just means that the server is not listening on the specified port
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Mar 15th, 2007, 12:20 AM   #3
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 372
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
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 !
xavier is offline   Reply With Quote
Old Mar 15th, 2007, 12:32 AM   #4
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 416
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
You might have made a mistake in the code responsible for getting the port number.
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC