Click here to Skip to main content
16,005,697 members
Home / Discussions / C#
   

C#

 
QuestionChanging Return Type Of A Control Pin
Roger Wright2-Jul-10 19:16
professionalRoger Wright2-Jul-10 19:16 
AnswerRe: Changing Return Type Of A Control Pin
OriginalGriff2-Jul-10 21:47
mveOriginalGriff2-Jul-10 21:47 
GeneralRe: Changing Return Type Of A Control Pin
Roger Wright3-Jul-10 6:47
professionalRoger Wright3-Jul-10 6:47 
AnswerRe: Changing Return Type Of A Control Pin
DaveyM692-Jul-10 21:59
professionalDaveyM692-Jul-10 21:59 
GeneralRe: Changing Return Type Of A Control Pin
Roger Wright3-Jul-10 6:53
professionalRoger Wright3-Jul-10 6:53 
QuestionDisplaying Contents of an Array Pin
ASPnoob2-Jul-10 13:19
ASPnoob2-Jul-10 13:19 
AnswerRe: Displaying Contents of an Array Pin
Luc Pattyn2-Jul-10 14:03
sitebuilderLuc Pattyn2-Jul-10 14:03 
QuestionClick here to send data to sever!! [modified] Pin
yum 20102-Jul-10 13:13
yum 20102-Jul-10 13:13 
hello everyone,
how can i send data to server??
I am using visual studio 2010.The program is working but i am not getting the option of " click here to send data to server" in the Form1 window that comes after client-server acknowledgment.Kindly guide me.

client program is

using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Text; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        NetworkStream serverStream; 

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            msg("Client Started");
            clientSocket.Connect("127.0.0.1", 8888);
            label1.Text = "Client Socket Program - Server Connected ...";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NetworkStream serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Message from Client$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            byte[] inStream = new byte[10025];
            serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);
            msg("Data from Server : " + returndata);
        }

        public void msg(string mesg)
        {
            textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
        } 
    }
}



the server program is


using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(8888);
            TcpClient clientSocket = default(TcpClient);
            int counter = 0;

            serverSocket.Start();
            Console.WriteLine(" >> " + "Server Started"); 

            counter = 0;
            while (true)
            {
                counter += 1;
                clientSocket = serverSocket.AcceptTcpClient();
                Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, Convert.ToString(counter));
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> " + "exit");
            Console.ReadLine();
        }
    }

    //Class to handle each client request separatly
    public class handleClinet
    {
        TcpClient clientSocket;
        string clNo;
        public void startClient(TcpClient inClientSocket, string clineNo)
        {
            this.clientSocket = inClientSocket;
            this.clNo = clineNo;
            Thread ctThread = new Thread(doChat);
            ctThread.Start();
        }
        private void doChat()
        {
            int requestCount = 0;
            byte[] bytesFrom = new byte[10025];
            string dataFromClient = null;
            Byte[] sendBytes = null;
            string serverResponse = null;
            string rCount = null;
            requestCount = 0;

            while ((true))
            {
                try
                {
                    requestCount = requestCount + 1;
                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);

                    rCount = Convert.ToString(requestCount);
                    serverResponse = "Server to clinet(" + clNo + ") " + rCount;
                    sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                    networkStream.Write(sendBytes, 0, sendBytes.Length);
                    networkStream.Flush();
                    Console.WriteLine(" >> " + serverResponse);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(" >> " + ex.ToString());
                }
            }
        }
    } 
}




please guide me.

Regards.

modified on Saturday, July 3, 2010 3:19 AM

AnswerRe: Click here to send data to sever!! Pin
Peace ON2-Jul-10 18:34
Peace ON2-Jul-10 18:34 
GeneralRe: Click here to send data to sever!! Pin
yum 20103-Jul-10 11:26
yum 20103-Jul-10 11:26 
QuestionAmazon API and C# Pin
tonyonlinux2-Jul-10 10:16
tonyonlinux2-Jul-10 10:16 
AnswerRe: Amazon API and C# Pin
Abhinav S2-Jul-10 17:57
Abhinav S2-Jul-10 17:57 
GeneralRe: Amazon API and C# Pin
tonyonlinux2-Jul-10 18:39
tonyonlinux2-Jul-10 18:39 
QuestionChange Row background color based by cell value Pin
grmihel22-Jul-10 5:14
grmihel22-Jul-10 5:14 
AnswerRe: Change Row background color based by cell value Pin
Henry Minute2-Jul-10 7:07
Henry Minute2-Jul-10 7:07 
AnswerRe: Change Row background color based by cell value Pin
Henry Minute2-Jul-10 8:37
Henry Minute2-Jul-10 8:37 
GeneralRe: Change Row background color based by cell value [modified] Pin
grmihel25-Jul-10 0:43
grmihel25-Jul-10 0:43 
GeneralRe: Change Row background color based by cell value Pin
Henry Minute5-Jul-10 3:34
Henry Minute5-Jul-10 3:34 
GeneralRe: Change Row background color based by cell value Pin
grmihel26-Jul-10 3:27
grmihel26-Jul-10 3:27 
QuestionI want a Clever Text Display Pin
Nagy Vilmos2-Jul-10 5:03
professionalNagy Vilmos2-Jul-10 5:03 
AnswerRe: I want a Clever Text Display Pin
R. Giskard Reventlov2-Jul-10 5:12
R. Giskard Reventlov2-Jul-10 5:12 
AnswerRe: I want a Clever Text Display Pin
OriginalGriff2-Jul-10 5:32
mveOriginalGriff2-Jul-10 5:32 
GeneralRe: I want a Clever Text Display Pin
Nagy Vilmos2-Jul-10 5:46
professionalNagy Vilmos2-Jul-10 5:46 
AnswerRe: I want a Clever Text Display Pin
OriginalGriff2-Jul-10 5:33
mveOriginalGriff2-Jul-10 5:33 
GeneralRe: I want a Clever Text Display Pin
Nagy Vilmos2-Jul-10 5:47
professionalNagy Vilmos2-Jul-10 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.