August 27th, 2003, 05:20 AM
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.
'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)
Console.WriteLine("TCP Server is up and waiting for Client connection...")
''Accept the pending client connection and return a TcpClient for communication.
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
' 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.
Catch e As Exception
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.
August 27th, 2003, 05:57 AM
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.