8 Replies - 1447 Views - Last Post: 15 June 2011 - 10:25 AM Rate Topic: -----

#1 miki_ir   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-June 11

why my client server not work : please look at code

Posted 14 June 2011 - 06:00 PM

client get 10 numberof int type , save it at array then send array to server
server show sum of all number in array .
program compile but in run time stop and not work coorrectly

--------------------------
Server Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Server
{
    class Server
    {

        static void Main(string[] args)
        {
            int[] b = new int[10];
            int recv;
            byte[] data = new byte[sizeof(int)];
            Socket sktListener, client;
            //-----------------
            sktListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint local_ip = new IPEndPoint(IPAddress.Any, 1200);
            try
            {
            sktListener.Bind(local_ip);
            sktListener.Listen(100);
            Console.WriteLine("Waiting for a client...");
            client = sktListener.Accept();
            IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
            Console.WriteLine("The Client Connected With This Address : {0} At This Port {1}", clientep.Address, clientep.Port);
            string welcome = "Welcome to my test server : You Are Now Connected";
            data = Encoding.ASCII.GetBytes(welcome);
            client.Send(data, data.Length, SocketFlags.None);
            //while (true)
            //{
            data = new byte[sizeof(int)];
            for (int i = 0; i <= 9; i++)
            {
                
                recv = client.Receive(data);
                if (recv == 0)
                    break;

                b[i] = int.Parse(Encoding.ASCII.GetString(data, 0, recv));
                Console.WriteLine(b[i]);

                //client.Send(data, recv, SocketFlags.None);
                if (i >= 10)
                {
                    break;
                }

            }
            Console.Write("the sum of array is: ");
            int sum = 0;
            for (int d = 0; d <= 9; d++)
            {
                Console.Write("{0}+", b[d]);
                sum += b[d];
            }
            Console.WriteLine();
            Console.WriteLine(sum);
            Console.Read();
            }
            catch (SocketException e)
            {
                Console.WriteLine("Unable to connect to server.");
                Console.WriteLine(e.ToString());
                return;
            }

            //Console.WriteLine("Disconnected from{0}",clientep.Address);
            //client.Close();
            //sktListener.Close();
            //-----------------

        }


    }
}

-----------------------------------
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 Client
{
    class Client
    {
        static void Main(string[] args)
        {
            //<gettin array of 10 int>
            int[] a = new int[10];
            for (int i = 0; i <= 9; i++)
            {
                Console.WriteLine("Enter Number {0} from 10 number", i + 1);
                a[i] = int.Parse(Console.ReadLine());
            }
            //</gettin array of 10 int>
            byte[] data = new byte[sizeof(int)];
            string input, stringData;
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1200);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                server.Connect(ipep);
                int recv = server.Receive(data);
                stringData = Encoding.ASCII.GetString(data, 0, recv);
                Console.WriteLine(stringData);
                for (int j = 0; j <= 9; j++)
                {
                    input = Convert.ToString(a[j]);
                    server.Send(Encoding.ASCII.GetBytes(input));
                }
                
                data = new byte[1024];
                recv = server.Receive(data);
                stringData = Encoding.ASCII.GetString(data, 0, recv);
                Console.WriteLine(stringData);
                Console.ReadLine();

            }
            catch (SocketException e)
            {
                Console.WriteLine("Unable to connect to server.");
                Console.WriteLine(e.ToString());
                return;
            }
        }
    }
}


/* while (true)
 {
     input = Console.ReadLine();
     if (input == "exit")
         break;
     server.Send(Encoding.ASCII.GetBytes(input));
     data = new byte[1024];
     recv = server.Receive(data);
     stringData = Encoding.ASCII.GetString(data, 0, recv);
     Console.WriteLine(stringData);
 }*/


Is This A Good Question/Topic? 0
  • +

Replies To: why my client server not work : please look at code

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: why my client server not work : please look at code

Posted 14 June 2011 - 06:36 PM

You need to EXPLAIN what is happening. You can't just tell us, "it's not working right". YOU need to tell us what is happening that is WRONG, and what is suppose to happen.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: why my client server not work : please look at code

Posted 14 June 2011 - 06:50 PM

Read this then this, then come back and ask this question again.
Was This Post Helpful? 1
  • +
  • -

#4 miki_ir   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-June 11

Re: why my client server not work : please look at code

Posted 14 June 2011 - 06:56 PM

View Posteclipsed4utoo, on 14 June 2011 - 06:36 PM, said:

You need to EXPLAIN what is happening. You can't just tell us, "it's not working right". YOU need to tell us what is happening that is WRONG, and what is suppose to happen.

client send 10 number to server and i see 10 of number at server screen ; then program wait : any thing not occur when i send another number client windows closed suddenly and program stop at
recv = client.Receive(data);

System.Net.Sockets.SocketException was unhandled
  Message=An existing connection was forcibly closed by the remote host
  Source=System
  ErrorCode=10054
  NativeErrorCode=10054
  StackTrace:
       at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
       at System.Net.Sockets.Socket.Receive(Byte[] buffer)
       at Server.Server.Main(String[] args) in C:\Documents and Settings\Pars\Desktop\Server.cs:line 41
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 


but i think
nothing work correctly becouse when is see number in server
i should see them like this(assume user inter this number)
21
1
23
12
23
34
5
34
12
0


but  i see at server

121
31231
52233
74534
9120



View PostCurtis Rutland, on 14 June 2011 - 06:50 PM, said:

Read this then this, then come back and ask this question again.

thanks
i insert code complete here . code is complete
you can if you like comile it
i dont now what i should to do
best regard

This post has been edited by miki_ir: 14 June 2011 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

#5 miki_ir   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-June 11

Re: why my client server not work : please look at code

Posted 14 June 2011 - 07:20 PM

i add before of
recv = client.Receive(data);

this code
Console.WriteLine("*");


to trace
-----------------------now see to result
i send with client this number : 12 1 33 444 1 2 3 2 1 34 to server . now i should see
at server thing like this :
*
12
*
1
*
33
*
...

but i see
*
12
*
1334
*
4412
*
3213
*
4
*


Was This Post Helpful? 0
  • +
  • -

#6 miki_ir   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-June 11

Re: why my client server not work : please look at code

Posted 14 June 2011 - 07:33 PM

when i send big number like
2232
3333
23232
...
program work good
i think problem is relate to size of send and receive what i should to do to program work with small number too
?
Was This Post Helpful? 0
  • +
  • -

#7 miki_ir   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-June 11

Re: why my client server not work : please look at code

Posted 14 June 2011 - 09:17 PM

client send array of int through socket to server

at client
int[] a = new int[10];
string input;
for (int j = 0; j <= 9; j++)
{
   input = Convert.ToString(a[j]);
   server.Send(Encoding.ASCII.GetBytes(input));
}
                


i receive data that client send by this code at server 


int[] b = new int[10];
int recv;
byte[] data = new byte[sizeof(int)];
for (int i = 0; i <= 9; i++)
{
      data = new byte[sizeof(int)];
      recv = client.Receive(data);
      b[i] = int.Parse(Encoding.ASCII.GetString(data, 0, recv));
}


program work good with big int number like 1234 67889 ,... but for small int like 2 34 ,...
not work .
problem is probably with buffering of bytes .when i send for example 2 3 4 because it transfer it by byte all of them collect as one number of int 234
what i should to do ?

This post has been edited by miki_ir: 14 June 2011 - 09:18 PM

Was This Post Helpful? 0
  • +
  • -

#8 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7506
  • View blog
  • Posts: 15,556
  • Joined: 16-October 07

Re: why my client server not work : please look at code

Posted 15 June 2011 - 04:23 AM

Your server seems to rely on everything sent after the connection to be exactly sizeof(int). If any of the data sent is less than that, you'll run into problems, because you're explicitly expecting that amount.

On the other side, the data you send is a little looser, being translated from the string. Here, you're not actually sending an int, but a string. You can store up to 255 in one byte, but if you send it as a string, it's three bytes('2','5','5').

I would advise you to accept as your server input a string with a linefeed at the end. Accept data until you get an empty line feed. Then go from there.

That, or encode an array of 10 digits and send it in one chunk...
Was This Post Helpful? 1
  • +
  • -

#9 miki_ir   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-June 11

Re: why my client server not work : please look at code

Posted 15 June 2011 - 10:25 AM

View Postbaavgai, on 15 June 2011 - 04:23 AM, said:

Your server seems to rely on everything sent after the connection to be exactly sizeof(int). If any of the data sent is less than that, you'll run into problems, because you're explicitly expecting that amount.

On the other side, the data you send is a little looser, being translated from the string. Here, you're not actually sending an int, but a string. You can store up to 255 in one byte, but if you send it as a string, it's three bytes('2','5','5').

I would advise you to accept as your server input a string with a linefeed at the end. Accept data until you get an empty line feed. Then go from there.

That, or encode an array of 10 digits and send it in one chunk...

thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1