Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.40/5 (3 votes)
See more:
hello...
My question is:

-Client:
1-send the Sum of 3 numbers:First,Second,Final Exam
2-Receive and Show a Letter From the user

-Server:
1-Receive number from the client
2-compare:
N<50→→→"D"
50>=N<60→→→"C"
60<=N<70→→→"B"
70<=N<80→→→"B+"
80<=N<90→→→"A"
90<=N<100→→→"A+"
3-Send the Letter to Client


Here is my c# Code:
C#
//server socket Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Data.OleDb;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The Server is Run And Wait .....");

            // Create Server Socket to recive
            TcpListener Ls = new TcpListener(7110);
            Ls.Start();

            //blocks until a client has connected to the server
            while (true)  // to make the server always on
            {
                // to connect with clients
                TcpClient client = Ls.AcceptTcpClient();
                NetworkStream clientStream = client.GetStream();
                byte[] message = new byte[4096];
                clientStream.Read(message, 0, message.Length);


                /////////////////////////////////////////////////

                string src = Encoding.ASCII.GetString(message, 0, message.Length);
                 
                
                
                OleDbConnection cnn = new OleDbConnection();
                cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\c++\\database.accdb";
                cnn.Open();

                OleDbDataAdapter adb = new OleDbDataAdapter("Select sip From Table1 Where Site='"+src.Trim('\0')+"'",cnn);
                System.Data.DataSet dat = new System.Data.DataSet();
                adb.Fill(dat);
                string Msgout = dat.Tables[0].Rows[0].ItemArray[0].ToString();

                //////////////////////////////////////////////////

                NetworkStream ServerStream = client.GetStream();
                byte[] buffer = Encoding.ASCII.GetBytes(Msgout);
                ServerStream.Write(buffer, 0, buffer.Length);

                ServerStream.Flush();
                client.Close();
 
            }
            Ls.Stop();

            

        }
    }
}
C#
//Client Socket Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;

namespace php1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            
            
              
        }

        private void button1_Click(object sender, EventArgs e)
        {

            // creat client Socket  to send
            TcpClient Sending = new TcpClient("localhost",7110);
            byte[] data =Encoding.ASCII.GetBytes(textBox1.Text);
            NetworkStream datastream = Sending.GetStream();
            datastream.Write(data, 0, data.Length);


            data = new byte[256];
            datastream.Read(data,0,data.Length);
            string ResponseData;
            ResponseData = Encoding.ASCII.GetString(data, 0, data.Length);



            textBox2.Text = textBox2.Text + Environment.NewLine + ResponseData;
            textBox1.Text = "";
            Sending.Close();


           
        

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //this.Close();
            Application.Exit();
        }
    }
}
[Edit]Code block added[/Edit]
Posted
Updated 30-Mar-13 4:54am
v3
Comments
Sergey Alexandrovich Kryukov 1-Apr-13 17:53pm    
I removed your another question (actually non-question) because it wasn't a question but was a re-post.
But you can comment the post bu Paulo Zemek and put your update to your question here, using "Improve question".
—SA

And your question is????

But from your code I spot 3 main problems.
1 - You never send or get the size of the message. TCP/IP is not a "message oriented" protocol, so you may send 10 bytes, but the other side may receive 3 bytes on the first read. So, you should be sending the size, then the content, and the other side must read the size, allocate the buffer with the right size, and then continue reading until it is guaranteed that all the message was received.

2 - Independent if you correct such error or not, locally the message will always arrive correctly. But then you are using GetString passing the full message length (which is your buffer with 4096 bytes). You should be passing the real message size (or on the worst case, the number of bytes returned by the clientStream.Read method).

3 - Your listener will process one client at a time. The correct will be to create a new thread when a connection is received, this way many clients can connect in parallel.
 
Share this answer
 
Comments
abed eshtiwi 30-Mar-13 11:40am    
[4096]....it's not real just example (for test only)
Member 10450892 8-Dec-13 8:13am    
Warning 1 'System.Net.Sockets.TcpListener.TcpListener(int)' is obsolete: 'This method has been deprecated. Please use TcpListener(IPAddress localaddr, int port) instead. http://go.microsoft.com/fwlink/?linkid=14202' c:\users\abreham\documents\visual studio 2012\Projects\clientservrerexample\serverapplication\Program.cs 19 30 serverapplication this warning message is come and the server is run but it is not worked the client side.
Paulo Zemek 30-Mar-13 11:46am    
The problem is not the value (1, 4096 or any other... the problem is that you are using the buffer length, not the amount of data really transferred).
Also, you never really made a question.
Sergey Alexandrovich Kryukov 1-Apr-13 17:53pm    
Good catch a, 5.
Please see my comment to the question: OP posted an updated, but I removed the re-post.
—SA
Member 11592499 9-Apr-15 4:32am    
gooooood work
We also need to know that TCP/IP sockets are stream based. Three send operations on the server side don't mean three read operations on the client side. Instead the client might get all the data in one or two read operations.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900