Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. tcp/ip socket

tcp/ip socket

Scheduled Pinned Locked Moved C#
sysadmin
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Harikrk
    wrote on last edited by
    #1

    i am working on a project to communicate with clinets using tcp/ip protocol. I have a server system where this program is being done. I have clinets which are connnected to my ip. These clients are capable of accepting commands from the server and gove reply for that. For eg:- if i give %Name to clinet it will send back the system ip address. private void button1_Click(object sender, EventArgs e) { Listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any,5005); Listener.Bind(ipLocal); Listener.BeginAccept(new AsyncCallback(OnClientConnect), null); } private void OnClientConnect(IAsyncResult asyn) { Client = Listener.EndAccept(asyn); WaitForData(); } private void WaitForData() { Client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null); } private void OnDataReceived(IAsyncResult asyn) { Byte[] sendBytes = Encoding.UTF8.GetBytes("%Name"); string szData; int iRx = 0 ; iRx = Client.EndReceive(asyn); char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(buffer, 0, iRx, chars, 0); szData = new string(chars); MessageBox.Show(szData.ToString()); Client.Send(sendBytes); WaitForData(); } The sending of data is working fine here. but when i revceive data they are all junk values even after converting to string. Junk vlaues means some single characters only Please let me know if tehre is someone extertise in this feild as i am new to this Thanks in advance Harikrishnan

    P 1 Reply Last reply
    0
    • H Harikrk

      i am working on a project to communicate with clinets using tcp/ip protocol. I have a server system where this program is being done. I have clinets which are connnected to my ip. These clients are capable of accepting commands from the server and gove reply for that. For eg:- if i give %Name to clinet it will send back the system ip address. private void button1_Click(object sender, EventArgs e) { Listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any,5005); Listener.Bind(ipLocal); Listener.BeginAccept(new AsyncCallback(OnClientConnect), null); } private void OnClientConnect(IAsyncResult asyn) { Client = Listener.EndAccept(asyn); WaitForData(); } private void WaitForData() { Client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null); } private void OnDataReceived(IAsyncResult asyn) { Byte[] sendBytes = Encoding.UTF8.GetBytes("%Name"); string szData; int iRx = 0 ; iRx = Client.EndReceive(asyn); char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(buffer, 0, iRx, chars, 0); szData = new string(chars); MessageBox.Show(szData.ToString()); Client.Send(sendBytes); WaitForData(); } The sending of data is working fine here. but when i revceive data they are all junk values even after converting to string. Junk vlaues means some single characters only Please let me know if tehre is someone extertise in this feild as i am new to this Thanks in advance Harikrishnan

      P Offline
      P Offline
      pbraun
      wrote on last edited by
      #2

      I've used: string receivedText = System.Text.Encoding.ASCII.GetString(buffer, 0, buffer.Count)); Of course this assumes that "buffer" in your case is a byte array. Also, please be sure to check what the client is supposed to be sending. Is it an array of values that need interpreting? Or is it a human readable string? Phil

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups