TCP/IP Bidirectional Programming Example Using Sockets - C#


Doc ID:    SA140
Version:    5.0
Published date:    11/29/2010
Updated:    09/12/2011
 

Content

The following is an illustration of how to communicate via a network connection using sockets in the C# programming language.

 
/**********************************************
 * CONFIDENTIAL AND PROPRIETARY
 *
 * The source code and other information contained herein is the confidential and the exclusive property of
 * ZIH Corp. and is subject to the terms and conditions in your end user license agreement.
 * This source code, and any other information contained herein, shall not be copied, reproduced, published,
 * displayed or distributed, in whole or in part, in any medium, by any means, for any purpose except as
 * expressly permitted under such license agreement.
 *
 * Copyright ZIH Corp. 2010
 *
 * ALL RIGHTS RESERVED
 ***********************************************/
 
//C# example illustrating bidirectional tcp/ip communications using System.Net.Sockets:
// Zebra Technical Support   
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
 
namespace Socket_example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
           
            System.Net.Sockets.TcpClient Zebraclient = new TcpClient();
            try
            {
               Zebraclient.SendTimeout = 1500;
               Zebraclient.ReceiveTimeout = 1500;
               //defining ip address and port number
               Zebraclient.Connect("10.17.50.202", 6101);
                //Zebraclient.Connect("10.17.50.202",9100);  
            }
            catch
            {
                MessageBox.Show("Not connected, verify connection");
            }
            if (Zebraclient.Connected == true)
            {
               //send and receive illustrated below
                MessageBox.Show("Connected!!");
                NetworkStream mynetworkstream;
                StreamReader mystreamreader;
               StreamWriter mystreamwriter;
               mynetworkstream = Zebraclient.GetStream();
                mystreamreader = new StreamReader(mynetworkstream);
                mystreamwriter = new StreamWriter(mynetworkstream);
                string commandtosend = "! U1 getvar ""comm.baud""";
                //string commandtosend = "~hs";
                mystreamwriter.WriteLine(commandtosend);
                mystreamwriter.Flush();
                char[] mk = null;
                mk = new char[20];
                mystreamreader.Read(mk,0,mk.Length);
                string data1 = new string(mk);
                textBox1.Text = data1;
                Zebraclient.Close();
            }
        }
    }
}

Rate this item

Find Answers

Find Answers

Zebra Partner Sign In

Registered Zebra Partners can sign in to view additional content

Did you forget your password?
Click here for assistance.