[ create a new paste ] login | about

Link: http://codepad.org/eKoc8evj    [ raw code | fork ]

C, pasted on Nov 19:
#if INTRODUCTION

By: Sevenstring
Demonstration of ATC1000M Module using C#'s

Version:
    00.08.00
        - First Release

#endif

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace ATC1000MDemo
{




    class Program
    {
        const string APP_VER="E-Gizmo TCP/IP Shield Loopback Test using C# - 00.08.00";


        static void Main(string[] args)
        {
        string tempBuffer;
        ATC1000M myDevice=new ATC1000M();

        Console.WriteLine(APP_VER);

        Console.WriteLine("\n\nOpening 192.168.2.1 Port 23...");
        myDevice.Open("192.168.2.1",23);
        Console.WriteLine("Opened!");


        Console.WriteLine("I will just test if there is a string on the buffer...");
        tempBuffer=myDevice.ReadString();
        if(tempBuffer!=null)
            {
            Console.WriteLine("Received String from Buffer: \n{0}",tempBuffer);
            }
        else
            {
            Console.WriteLine("No String Received!");
            }

        Console.WriteLine("\n\nC# Now Sending a String... Please wait...\n\n");
        myDevice.Write("Hello World! This string sent through TCP/IP by C#!\nIt this is displayed, the test is successful! Congrats!");
        Thread.Sleep(1000);

        tempBuffer=myDevice.ReadString();
        if(tempBuffer!=null)
            {
            Console.WriteLine("Received String from Buffer! \n{0}",tempBuffer);
            }
        else
            {
            Console.WriteLine("No String Received!");
            }
        
        Console.WriteLine("\n\nClosing the connection and the streams...");
        myDevice.Close();
        Console.WriteLine("Closed!");
        }// end of static void Main(string[] args)
    }










    public class ATC1000M
    {
        TcpClient tcpclient=null;
        NetworkStream output;
        BinaryWriter writer;
        BinaryReader reader;

        public bool Open(string hostname,int port)
        {
        tcpclient=new TcpClient();
        tcpclient.Connect(hostname,port);
        output=tcpclient.GetStream();
        writer=new BinaryWriter(output);
        reader=new BinaryReader(output);
        reader.BaseStream.ReadTimeout=500;
        return tcpclient.Connected;      
        }


        public void Write(string data)
        {
        writer.Write(data);
        }


        public string ReadString()
        {
        string retString;

        try
            {
            retString=reader.ReadString();
            }
        catch(System.IO.IOException)
            {
            retString=null;
            }
        return retString;
        }


        public void Close()
        {
        writer.Dispose();
        reader.Dispose();
        output.Dispose();
        tcpclient.Close();
        }
    }//end of public class ATC1000M

}


Create a new paste based on this one


Comments: