Results 1 to 5 of 5

Thread: Socket Client/Server

  1. #1
    Join Date
    Aug 2009
    Posts
    54

    Socket Client/Server

    I want to make a program so that the server reads from a file (c:/test.txt) and send data to the client, and it writes to another file (c:/info2.txt).

    The problem this poses is that I could not figure out that when the client stops receiving, it should call me to send nothing but how to do a program like that?

    here is the my source code


    Client:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    namespace Script_Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                TcpClient c = new TcpClient("localhost", 8080);
                Stream str = c.GetStream();
                byte[] b=new byte[100];
                string line = null; 
                StreamWriter fluxInfos = null; 
                try
                {
                    fluxInfos = new StreamWriter("c:/infos2.txt" );
                        while(str.Read(b,0,50)!=-1)
                        {
                                line = Encoding.ASCII.GetString(b);
                                Console.WriteLine(line);
                                fluxInfos.WriteLine(line);
                        }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The following error occurred: " + e.Message);
                }
                Console.ReadLine();
            }
        }
    }
    Server:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    namespace Play
    {
        class Program
        {
            static void Main(string[] args)
            {
                IPHostEntry iphe = Dns.Resolve("localhost" );
                IPEndPoint ipep = new IPEndPoint(iphe.AddressList[0], 8080);
                TcpListener s = new TcpListener(ipep);
                s.Start();
                TcpClient c = s.AcceptTcpClient();
                Stream str = c.GetStream();
                byte[] b;
                String line = null;
                StreamReader fluxInfos = null;
                try
                {
                    fluxInfos = new StreamReader("c:/Test.txt" );
                        line = fluxInfos.ReadLine();
                        while (line != null)
                        {
                            Console.WriteLine(line);
                            b = Encoding.ASCII.GetBytes(line);
                            str.Write(b, 0, b.Length);
                            line = fluxInfos.ReadLine();
                        }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The following error occurred: " + e.Message);
                }
                System.Console.ReadLine();
            }
        }
    }

  2. #2
    Join Date
    Nov 2008
    Posts
    1,185

    Re: Socket Client/Server

    You can try to use a special character to indicate the end. You can use a special character unprintable, it seems that there is a character to signal the end of file (EOF).Basically there are special characters that are not friendly, throw a glance on the side of the ASCII table.

  3. #3
    Join Date
    Aug 2009
    Posts
    54

    Re: Socket Client/Server

    Yes I can but it's not a very good solution. If I use $ then I have a $ in my text, its definitely going to be a problem. I'm not very surprised but I know the special characters are unprintable. Its mean anything?

  4. #4
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Socket Client/Server

    In fact you have in principle nothing to do knowing that you work with the flow. The method "Read" returns "0" when the flow is finished: http://msdn.microsoft.com/en-us/libr...ream.read.aspx

    The read method return the total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

  5. #5
    Join Date
    Aug 2009
    Posts
    54

    Re: Socket Client/Server

    I made a new program that allows me to read line by line at the server (StreamReader / StreamWriter) and sent to the client, the problem that this poses is that the customer does not always know when it stops, it turns into a infinite loop but server stops at end of file:

    Client:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    namespace Script_Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                TcpClient c = new TcpClient("localhost", 8080);
                NetworkStream networkStream = c.GetStream();
                StreamReader streamReader = new StreamReader(networkStream);
                String theString;           
                StreamWriter fluxInfos = null;
                try
                {
                    fluxInfos = new StreamWriter("c:/infos2.txt" );
                    theString = streamReader.ReadLine();
                    while (theString!="" )
                        {
                            Console.WriteLine(theString);
                            theString = streamReader.ReadLine();
                             fluxInfos.WriteLine(theString);
                        }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The following error occurred: " + e.Message);
                }
                streamReader.Close();
                networkStream.Close();
                c.Close();
            }
        }
    }
    Server:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    namespace Play
    {
        class Program
        {
            static void Main(string[] args)
            {
                IPHostEntry iphe = Dns.Resolve("localhost" );
                IPEndPoint ipep = new IPEndPoint(iphe.AddressList[0], 8080);
                TcpListener s = new TcpListener(ipep);
                s.Start();
                TcpClient c = s.AcceptTcpClient();
                NetworkStream networkStream = c.GetStream();
                StreamWriter streamWriter = new StreamWriter(networkStream);
                streamWriter.AutoFlush = true;
                String line = null;
                StreamReader fluxInfos = null;
                try
                {
                    fluxInfos = new StreamReader("c:/Test.txt" );
                        line = fluxInfos.ReadLine();
                        while (line!= null)
                        {
                            Console.WriteLine(line);
                            streamWriter.WriteLine(line);
                            line = fluxInfos.ReadLine();
                        }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The following error occurred: " + e.Message);
                }
                networkStream.Close();
                streamWriter.Close();
                //c.Close();
                System.Console.ReadLine();
            }
        }
    }

Similar Threads

  1. Replies: 6
    Last Post: 22-07-2011, 10:24 PM
  2. connection to server failed socket error 10013
    By leijen in forum Windows Vista Mail
    Replies: 1
    Last Post: 25-04-2011, 11:04 AM
  3. Replies: 7
    Last Post: 16-02-2011, 08:14 PM
  4. UDP socket client / server
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 01:33 AM
  5. What is socket and server socket in java?
    By Truster in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 02:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,718,235,114.16955 seconds with 16 queries