Results 1 to 4 of 4

Thread: Binary sockets in C#

  1. #1
    Join Date
    Nov 2008
    Posts
    68

    Binary sockets in C#

    Hi, I need to learn how to receive streaming binary data and also send it to remote computers on a network with the help of C#. If you are able to provide me any link or books related to this topic then also this will help me. Please give me reply. I want to learn it for improving my knowledge in c#. Thank you in advance.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Binary sockets in C#

    Hi, I am also wanted to know same thing from many days, unable to do it. If you find it just post it for me also. It is quiet difficult. I have tried for it but can't get solution regarding it. Just wanted to know how to do this. Steps or something also welcome. I just want to know how to do this. I can do it come to know some or the other steps about it. Please help me also. Just post on this forum if anyone find regarding it.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Binary sockets in C#

    Hi, I don't know anything about it. But want to learn it as it sounds interesting. Just help with some tutorials if you are having it. It is not possible to search on net as it is not showing anything regarding it. Can you just tell what it is exactly. Just give me reply. I want to learn programming deeply and have knowledge of c# but at beginners level. Please try to provide the tutorials or books which have the starting with beginning level.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Binary sockets in C#

    Hi guys, not having any knowledge about Binary Sockets in c#, but got something regarding it on internet which I am posting for you. If you are getting something from it then ok else sorry.

    code for client:

    using System;
    using System.Net.Sockets;
    using System.Windows.Forms;
    using System.IO ;
    using System.ComponentModel ;
    using System.Drawing;
    public class Echoclient: Form
    {
    //Define the components...
    private Button b1;
    private TextBox t1,ta;
    TcpClient myclient;
    private NetworkStream networkStream ;
    private StreamReader streamReader ;
    private StreamWriter streamWriter ;

    //constructor initialising...
    public Echoclient()
    {
    InitializeComponent();
    }
    //main method...
    public static void Main()
    {
    Echoclient df=new Echoclient();
    df.FormBorderStyle=FormBorderStyle.Fixed3D;
    Application.Run(df);
    }
    public void InitializeComponent()
    {
    b1=new Button();
    b1.Location = new System.Drawing.Point(170,20);
    b1.Size = new System.Drawing.Size (80,40);
    b1.Text="Click Here";
    b1.Click += new System.EventHandler(b1_Click);
    b1.BackColor = System.Drawing.Color.Transparent ;
    b1.ForeColor = System.Drawing.Color.Red ;
    b1.BackgroundImage = Image.FromFile("back4.gif") ;
    b1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    b1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8f, System.Drawing.FontStyle.Bold);

    t1=new TextBox();
    t1.Location = new System.Drawing.Point(20,20);
    t1.Size = new System.Drawing.Size (100,100);

    ta=new TextBox();
    ta.Multiline=true;
    ta.ScrollBars = ScrollBars.Vertical;
    ta.AcceptsReturn = true;
    ta.AcceptsTab = true;
    ta.WordWrap = true;
    ta.Location = new System.Drawing.Point(20,80);
    this.SuspendLayout();
    this.Text = "Socket Programming" ;
    this.MaximizeBox = false;
    this.BackgroundImage = Image.FromFile("back3.gif") ;
    this.Name = "Form1";
    this.Controls.Add(this.b1);
    this.Controls.Add(this.t1);
    this.Controls.Add(this.ta);
    this.Closing+= new CancelEventHandler(form1_closing) ;
    //connect to the "localhost" at the give port
    //if you have some other server name then you can use that instead of "localhost"
    try
    {
    myclient = new TcpClient("localhost", 1234);
    }
    catch
    {
    Console.WriteLine("Failed to connect to server at {0}:999", "localhost");
    return;
    }
    //get a Network stream from the server
    networkStream = myclient.GetStream();
    streamReader = new StreamReader(networkStream);
    streamWriter = new StreamWriter(networkStream);
    }
    //User can enter a text in the textbox and on click of the button the message will be sent to the server..
    //then the Client waits and receives the response from the server which is displayed in the textarea..
    private void b1_Click(object sender, EventArgs e)
    {
    ta.Text="" ;
    if(t1.Text=="")
    {
    MessageBox.Show("Please enter something in the textbox");
    t1.Focus();
    return ;
    }
    try
    {
    string s;
    streamWriter.WriteLine(t1.Text);
    Console.WriteLine("Sending Message");
    streamWriter.Flush();
    s= streamReader.ReadLine();
    Console.WriteLine("Reading Message") ;
    Console.WriteLine(s) ;
    ta.Text=s;

    }
    catch(Exception ee)
    {
    Console.WriteLine("Exception reading from Server:"+ee .ToString());
    }

    }
    public void form1_closing(object o , CancelEventArgs ec)
    {
    //close all streams...
    streamReader.Close() ;
    streamWriter.Close() ;
    networkStream.Close();
    }
    }



    code for server:

    #include <QtGui>
    #include <QtNetwork>

    #include <stdlib.h>

    #include "server.h"

    Server:: Server(QWidget *parent)
    : QDialog(parent)
    {
    statusLabel = new QLabel;
    quitButton = new QPushButton(tr("Quit"));
    quitButton->setAutoDefault(false);

    tcpServer = new QTcpServer(this);
    if (!tcpServer->listen()) {
    QMessageBox::critical(this, tr("Fortune Server"),
    tr("Unable to start the server: %1.")
    .arg(tcpServer->errorString()));
    close();
    return;
    }

    statusLabel->setText(tr("The server is running on port %1.\n"
    "Run the Fortune Client example now.")
    .arg(tcpServer->serverPort()));

    fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
    << tr("You've got to think about tomorrow.")
    << tr("You will be surprised by a loud noise.")
    << tr("You will feel hungry again in another hour.")
    << tr("You might have mail.")
    << tr("You cannot kill time without injuring eternity.")
    << tr("Computers are not intelligent. They only think they are.");

    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(quitButton);
    buttonLayout->addStretch(1);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(statusLabel);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);

    setWindowTitle(tr("Fortune Server"));
    }

    void Server::sendFortune()
    {
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);
    out << (quint16)0;
    out << fortunes.at(qrand() % fortunes.size());
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));

    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()),
    clientConnection, SLOT(deleteLater()));

    clientConnection->write(block);
    clientConnection->disconnectFromHost();
    }

Similar Threads

  1. First mainboard created with two sockets
    By Anna_relic in forum Motherboard Processor & RAM
    Replies: 2
    Last Post: 04-01-2012, 07:41 PM
  2. AM2+ vs AM3 sockets
    By Xymaya in forum Hardware Peripherals
    Replies: 6
    Last Post: 21-11-2010, 05:39 AM
  3. Problem with Sockets and Protocols
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 06:23 AM
  4. Apartment without RJ45 sockets
    By DotNetUser in forum Networking & Security
    Replies: 3
    Last Post: 19-10-2009, 09:28 AM
  5. Sockets in python
    By Benito in forum Software Development
    Replies: 2
    Last Post: 19-08-2008, 01:15 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,713,557,430.79832 seconds with 17 queries