Subject: data transfer via socket
Date: 2012-08-30 13:07:26
From: Ahmad Nosrati
Source: data-transfer-via-socket
----------------------------------------------------------------------

 

Hello,
I want to write for a barcode scanner with Win CE 5 a small program that the file.txt on the scanner is transmitted through socket on the target computer.
 
My program looks like this:
 
 
[code lang='c#']
using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
 
namespace SocketTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ComponentPro.Net.ProxyClient sock = new ComponentPro.Net.ProxyClient(AddressFamily.InterNetwork);
                IPAddress test = IPAddress.Parse("192.168.2.2");
                sock.Connect(test, 500);
                NetworkStream nsr = sock.GetStream();
 
                if (nsr.CanWrite)
                {
                    Stream reader = File.OpenRead("file.txt");
                    byte[] buf = new byte[reader.Length];
                    reader.Read(buf, 0, buf.Length);
                    nsr.Write(buf, 0, buf.Length);
                    nsr.Close();
                    reader.Close();
                    sock.Close();
                }
                else
                {
                    Console.WriteLine("You cannot write data to this stream.");
                    sock.Close();
                    nsr.Close();
                    return;
                }
            }
            catch
            {
            }
        }
    }
}
[/code]
 
Would that be correct and it would work this way?
 
 
With best regards,
A.N.
---------------------------------------------------------------------- Note: This question has been asked on the Q&A forum of Thang Dang's fraudulent ComponentPro brand If you purchased anything from ComponentPro, you have been scammed. Contact the payment processor who sold you the license and ask for your money back. Back to ComponentPro Q&A Forum Index