Would really appreciate some help with programming, mostly socket type

I'm really sorry to post here so much. I've been looking alot online and in books for how to do things in Visual Basic, but I haven't been finding the answers I want. I deleted the earlier post about the IRC bot so I wouldn't take up too much space on the forum.

If this forum is mostly for professionals, I apologize, and I can try to find somewhere else. I consider myself fairly intelligent, but I've really been struggling with trying to figure out how to do alot of things in VB. I'm adding my email address to my profile so anyone can contact me if you are willing to help. I also host an IRC server that I'd like to start growing about VB programming at braab.myftp.org. That would be the best medium for me to discuss specific questions I have with someone willing to help. I'm mostly interested in socket programming, and I'm currently trying to see if I can get a messenger program working, just to learn. Thanks for being patient with me.



Answer this question

Would really appreciate some help with programming, mostly socket type

  • steve thomas

    hi,

    first of all this forum is not for experts only , most of us here helping each other to be experts, may be your question about irc wasn't in area that anyone was interested in , because really it depend on your understanding for irc packets which is not related to vb at all

    networking applications depend on two sides not one , your code just for one side and mixing up tcplistner with tcpclient, you have to split it up to 2 projects both of them should run at the same time make a server and compile it use command prompt and run your server from yourProjectFolder/bin/debug

    then creat the client side application and run it through debugging the two programs will look like this



    Class SimpleTCpListner

    Public Shared Sub entry()

    'server use any ip address on the local machine

    'just because i'll try this on the local network

    'or i can specify particular ipaddress i want in real program by useing ipaddres.parse("ip as string ")

    Dim srvr As TcpListener = New TcpListener(IPAddress.Any, 9050)
    srvr.Start
    Console.WriteLine(
    "Waiting for Clients to connect ..")
    Dim clnt As TcpClient = srvr.AcceptTcpClient
    Dim ns As NetworkStream = clnt.GetStream
    Dim wlcom As String = "welcome to simple TCP listener server ..."
    Dim strdata As String
    Dim
    data() As Byte = New Byte(1024) {}
    Dim recv As Integer
    data = Encoding.ASCII.GetBytes(wlcom)
    ns.Write(data,
    0, data.Length)
    ns.Flush

    While true
    data = New Byte(1024) {}
    recv
    = ns.Read(data, 0, data.Length)
    If (recv = 0) Then
    Exit While
    End If
    strdata = Encoding.ASCII.GetString(data, 0, recv)
    Console.WriteLine(strdata)
    ns.Write(data,
    0, recv)

    End While
    ns.Close
    clnt.Close
    srvr.
    Stop
    End Sub
    End Class


    the client side will looks something like this




    class=code>Class A6_HlprClassesTCpClient

    Public Shared Sub entry()
    'creat a new client to the server
    Dim srvr As TcpClient
    Try
    'server ip and port number
    '127.0.0.1 is local ip you can set a network ip if you will try this program on network
    srvr = New TcpClient("127.0.0.1", 9050)
    Catch ex As SocketException
    Console.WriteLine(
    "Cann't connect to server ...")
    Console.WriteLine(ex.Message)
    Return
    End Try
    ' data holders
    Dim data() As Byte = New Byte(1024) {}
    Dim strData As String
    Dim input As String
    Dim
    recv As Integer
    Dim
    ns As NetworkStream = srvr.GetStream
    class=code>
    'recieve wellcome message
    recv = ns.Read(data, 0, data.Length)
    strData
    = Encoding.ASCII.GetString(data, 0, recv)
    Console.WriteLine(strData)
    'start sending data and recieve answers

    While true
    input =
    Console.ReadLine
    If (input = "exit") Then
    Exit While
    End If
    data = Encoding.ASCII.GetBytes(input.ToCharArray)
    ns.Write(data,
    0, data.Length)
    ns.Flush
    data
    = New Byte(1024) {}
    recv
    = ns.Read(data, 0, data.Length)
    strData
    = Encoding.ASCII.GetString(data, 0, recv)
    Console.WriteLine(strData)

    End While
    'close every thing
    Console.WriteLine("Disconnected from server ... ")
    ns.Close
    srvr.Close
    End Sub
    End Class

    don't forget to import those namespaces at the top of either classes

    Imports System.Text

    Imports System.Net

    Imports System.Net.Sockets

    i think the right place to ask about networking applications is this forum http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=40&SiteID=1

    hope this helps



  • Imran Aziz

    Thanks. I have yet to try that out, but I really appreciate the help. I copied the code in and there are no errors, so it's looking good. My offer still stands to anyone who wants to join my IRC server. I don't really have a user base yet, but I'd like to get one built up. One more quick question, even though dealing with that is awhile off. Would it be possible for my LAN messenger program to have the capability of sending a picture and having that picture appear in the picture box of the other computer Thanks.
  • Albin

    I will post just one more question, and I'll put it as a reply to this thread, so as not to take up alot of space.  I've never figured this out.  When using System.Net.IPAddress or System.Net.IPEndPoint, what form are you supposed to put the IP in   I'm trying to adapt the code from here (http://www.eggheadcafe.com/articles/20020323.asp) to make a messenger program, and I can't figure out how and where to put the IP and port without getting errors.  Here's what I have:

    Imports System.Net.Sockets

    Imports System.Text

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim LocalAddr As System.Net.IPEndPoint

    Dim MyListener As TcpListener

    MyListener.Start()

    MessageBox.Show(MyListener.LocalEndpoint.ToString)

    Label1.Text = "Waiting for connection..."

    Try

    Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()

    Label1.Text = "Connection accepted."

    Dim networkStream As NetworkStream = tcpClient.GetStream()

    Dim bytes(tcpClient.ReceiveBufferSize) As Byte

    networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

    Dim clientdata As String = Encoding.ASCII.GetString(bytes)

    TextBox1.Text = "Client sent: " + clientdata

    Dim responseString As String = "Connected to server."

    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)

    networkStream.Write(sendBytes, 0, sendBytes.Length)

    Label1.Text = "Message Sent /> : " + responseString

    tcpClient.Close()

    tcpListener.Stop()

    Label1.Text = "exit"

    Console.ReadLine()

    Catch ex As Exception

    Console.WriteLine(e.ToString())

    Console.ReadLine()

    End Try

    End Sub

    End Class

    For now, I'd like to use the localhost as the IP to connect to, on my client side of this, and if I need an IP on the server side here, I guess I'd use the same.

     


  • Would really appreciate some help with programming, mostly socket type