another visual studio question
Results 1 to 2 of 2

Thread: another visual studio question

  1. #1
    Junior Member
    Join Date
    Aug 2003
    Posts
    29

    Angry another visual studio question

    ok i figured out that in visual studio .NET that i had to program my own windows socket so i did a search on googlie and found a tut. the only problem is that when i open it up and run it, it will just close after being open for maybe a second or two. im tryin to make a chat program for multiple users. XP. thanks for any help.



    Imports System.Net.Sockets
    Imports System.Net
    Imports System.Text
    Imports System.Net.DnsPermissionAttribute
    Imports System.Security.Permissions
    Module Module1

    'DnsPermissionAttribute specifies permission to request information from Domain Name Servers.
    <DnsPermissionAttribute(SecurityAction.Demand, Unrestricted:=True)> Class CTestTCPServer
    Shared Sub Main()
    'Server must be on the same port that the client is connected on.
    Const portNumber As Integer = 8000

    '"Localhost" string is used when the client and the listener are on the same computer.
    'If the server is listening at a computer that is different from the client, then provide the host name of the computer
    'where the server is listening.
    'Dim tcpListener As New TcpListener(CType(Dns.Resolve("DDE_Chatting").AddressList(0),192.168.35.53), 8000)
    'Comment the previous line and uncomment the following line if you are using Visual Basic .NET (2003).
    Dim tcpListener As New TcpListener(8000)
    tcpListener.Start()
    Console.WriteLine("TCP Server is up and waiting for Client connection...")
    Try

    ''Accept the pending client connection and return a TcpClient for communication.
    Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
    Console.WriteLine("Connection accepted.")
    ' Get the data stream.
    Dim networkStream As NetworkStream = tcpClient.GetStream()
    ' Read the data stream into a byte array.
    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
    ' Return the data received from the client to the console.
    Dim clientdata As String = Encoding.ASCII.GetString(bytes)
    Console.WriteLine(("Client sent: " + clientdata))
    Dim responseString As String = "Successfully connected to TCP server."
    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
    networkStream.Write(sendBytes, 0, sendBytes.Length)
    Console.WriteLine(("Message Sent by TCP Server /> : " + responseString))
    'Close TcpListener and TcpClient.
    tcpClient.Close()
    tcpListener.Stop()
    Console.WriteLine("Exit")
    Console.ReadLine()
    Catch e As Exception
    Console.WriteLine(e.ToString())
    Console.ReadLine()
    End Try
    End Sub
    End Class
    Sub Main()

    End Sub

    End Module


    i dont know if there is any special format to put code in so my bad if its wrong.
    Stay away from my friends, they\'re smooth operators lookin for a way in.

  2. #2
    Senior Member
    Join Date
    Jul 2003
    Posts
    114
    It looks like your program is missing something vital: a loop. Or two. Maybe more. And a call to select, or perhaps some lovely threads.

    AFAICT, you code check to see if it has a remote connection, then blithely continues on whether it does or not, then sends some data to this hypothetical connection, then closes it all up and goes away. Not sure, I don't have an API reference handy.

    Basically, I think you're being a bit overambitious. Try writing a client that accepts input and writes it to a file, first. Then perhaps try writing another program that reads from that file as it's being written. Then merge the two programs together. After that, try writing your char program again.

    And any way, with the structure you're using, you'll only be able to support two users.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides