Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

PHP Chat changing to socket chat, if possible. Help me please

Hello fellow .NET programmers. So i'm currently working on a chat, like Skype and i got it working! But since it's using PHP and it is making requests to my website each second it's a bit laggy. Then i used threads, and it certainly helped a lot, but just not enough. So earlier today i read about sockets, and if i understood it correctly i should be able to create a server, and of course a client that listens for new chat strings? How am i gonna do this? I hope that some of you guys can help me in the right direction. Of course i'm not looking for source code ready for CP. I'm here for learning!
A Php chat is highly inefficient on your server since you're querying it every second, yes you should use sockets and not a web request since that's bad server side. What you should do is setup the server as the server all the clients connect to.
It's a vb.net application that makes request to my mysql through php. I dont care about security right now either. I wanna know how it's working, before i go into security.

It should be

client -> server -> client
(Feb 1, 2016 11:13 AM)Flzduden Wrote:  It's a vb.net application that makes request to my mysql through php. I dont care about security right now either. I wanna know how it's working, before i go into security.

It should be

client -> server -> client

Well if that's what you're looking for then I made this some time ago,
https://hackforums.net/showthread.php?tid=4615781

Though I wouldn't recommend it, I'd recommend using sockets, but I guess it's good for learning purposes.
(Feb 1, 2016 11:16 AM)+Haze Wrote:  Well if that's what you're looking for then I made this some time ago,
https://hackforums.net/showthread.php?tid=4615781

Though I wouldn't recommend it, I'd recommend using sockets, but I guess it's good for learning purposes.

Yeah sockets is exactly what i'm trying to learn, i got totally misunderstood (probably my shitty english lol) I already HAVE made a php chat, but it's quite unstable.
Code
Private Sub formMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim server As TcpListener = New TcpListener(IPAddress.Parse("127.0.0.1"), 1336)
        server.Start()

        server.BeginAcceptTcpClient(New AsyncCallback(AddressOf OnAccept), server)
    End Sub

    Sub OnAccept(AsyncResult As IAsyncResult)
        Try
            Dim server As TcpListener = DirectCast(AsyncResult.AsyncState, TcpListener)
            Dim client As TcpClient = server.EndAcceptTcpClient(AsyncResult)
            server.BeginAcceptTcpClient(New AsyncCallback(AddressOf OnAccept), Nothing)

            Dim readThread As New Thread(AddressOf Read)
            readThread.Start(client)
        Catch ex As Exception : End Try
    End Sub

    Sub Read(Client As TcpClient)
        Try
            Using reader As New StreamReader(Client.GetStream())
                Dim data As String = reader.ReadToEnd
            End Using
        Catch ex As Exception : End Try
    End Sub

    Private Async Sub SendMessage(Message As String)
        Try
            Dim client As TcpClient = New TcpClient("127.0.0.1", 1337)

            Using writer As New StreamWriter(client.GetStream())
                Await writer.WriteAsync(Message)
            End Using
        Catch ex As Exception : End Try
    End Sub

Some code I had laying around. This should give you the general idea.
(Feb 2, 2016 09:52 AM)Ninja Wrote:  
Code
Private Sub formMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim server As TcpListener = New TcpListener(IPAddress.Parse("127.0.0.1"), 1336)
        server.Start()

        server.BeginAcceptTcpClient(New AsyncCallback(AddressOf OnAccept), server)
    End Sub

    Sub OnAccept(AsyncResult As IAsyncResult)
        Try
            Dim server As TcpListener = DirectCast(AsyncResult.AsyncState, TcpListener)
            Dim client As TcpClient = server.EndAcceptTcpClient(AsyncResult)
            server.BeginAcceptTcpClient(New AsyncCallback(AddressOf OnAccept), Nothing)

            Dim readThread As New Thread(AddressOf Read)
            readThread.Start(client)
        Catch ex As Exception : End Try
    End Sub

    Sub Read(Client As TcpClient)
        Try
            Using reader As New StreamReader(Client.GetStream())
                Dim data As String = reader.ReadToEnd
            End Using
        Catch ex As Exception : End Try
    End Sub

    Private Async Sub SendMessage(Message As String)
        Try
            Dim client As TcpClient = New TcpClient("127.0.0.1", 1337)

            Using writer As New StreamWriter(client.GetStream())
                Await writer.WriteAsync(Message)
            End Using
        Catch ex As Exception : End Try
    End Sub

Some code I had laying around. This should give you the general idea.

Why are you catching an Exception only to ignore it? Tut tut tut
(Feb 2, 2016 09:52 AM)Ninja Wrote:  
Code
Catch ex As Exception : End Try

:(

You're also mixing between sync and async in that snippet.
(Feb 1, 2016 11:16 AM)+Haze Wrote:  Well if that's what you're looking for then I made this some time ago,
https://hackforums.net/showthread.php?tid=4615781

Though I wouldn't recommend it, I'd recommend using sockets, but I guess it's good for learning purposes.

You have some PHP errors in the scripts 😠
Well they're more logical mistakes
I wrote that code literally last year ago, even I don;t recommend that code.
Thanks for all the inputs. I found a few tutorials on the www, but i just dont get it really. All of those tut's can only send data one way. Like from the client to the server, or from the server to multiple clients. The client shouldn't act as a server in any way right? Cuz then all of those clients would have to port forward, and that's not the idea really.. Only the server host should portforward. Am i right on this?
(Feb 2, 2016 03:34 PM)Flzduden Wrote:  Thanks for all the inputs. I found a few tutorials on the www, but i just dont get it really. All of those tut's can only send data one way. Like from the client to the server, or from the server to multiple clients. The client shouldn't act as a server in any way right? Cuz then all of those clients would have to port forward, and that's not the idea really.. Only the server host should portforward. Am i right on this?

Think of it as reverse connection, just like with rats. It's still possible to communicate data between the client and server.
(Feb 2, 2016 03:37 PM)Odus Wrote:  Think of it as reverse connection, the end users connect to you, just like with rats. It's still possible to communicate data between the client and server.

wow, didn't think of it that way. I think i'm in the right direction now, i got i sending from client to server - and from server to client now. That's kinda what i would acheive from it. So thanks to everyone helping. Such a great forum.
(Feb 2, 2016 12:44 PM)+Haze Wrote:  Why are you catching an Exception only to ignore it? Tut tut tut

its just something I wrote quick to test sockets.

(Feb 2, 2016 01:17 PM)Odus Wrote:  :(

You're also mixing between sync and async in that snippet.

See previous post.
(Feb 2, 2016 09:52 AM)Ninja Wrote:  
Code
Catch ex As Exception : End Try

Some code I had laying around. This should give you the general idea.

At least know what you're doing before you start giving other people bad habits.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by F.A.S.TApr 16, 2018 09:44 AM 3 171 Apr 16, 2018 09:44 AM
Last Post: Goodies
[.NET] by jessman420Oct 4, 2015 04:28 AM 6 293 Oct 4, 2015 04:28 AM
Last Post: YaBoii
[.NET] by ChessoMar 11, 2015 08:50 AM 7 288 Mar 11, 2015 08:50 AM
Last Post: ElementalTree