Click here to Skip to main content
15,881,561 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi, Thanks for taking your time and try to help me, first of all, excuse my bad english.

Well, I want to stablish a service where multiple clients sends msgs to a server, but with non permanent connection. I have made a conection between 2 pcs, but i'll try to explain with images what i want:

1.- Server is up. First Client make his socket connection:

http://imageshack.com/a/img841/8246/fkyu5.jpg[^]

2.- Second client make his connection:
http://imagizer.imageshack.us/a/img822/3308/v4a8.jpg[^]

3.- Third client make his connection and first client close connection:

http://imageshack.com/a/img829/4230/hwlo.jpg[^]

That's almost what I want to do.

I'll put here my code to let you know what Am I working with

Client Code:

VB
Imports System.Net.Sockets
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Try

            'Creamos la conexión
            Dim oSocket As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

            'Tomamos los datos. Límite de buffer: 500
            Dim vDatos(500) As Byte

            vDatos = System.Text.Encoding.BigEndianUnicode.GetBytes(TextBox1.Text)

            'Conectamos con el servidor: sabemos donde está
            oSocket.Connect(Net.IPAddress.Parse("192.168.1.100"), 60000)
            'oSocket.Connect("localhost", 60000)

            'Enviamos los datos
            oSocket.Send(vDatos)

            'Nos desconectamos del servidor
            oSocket.Disconnect(False)

            'Cerramos el socket usado para el envío
            oSocket.Close()

        Catch er As SocketException

            MsgBox(er.SocketErrorCode)

        End Try

    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub
End Class


Server Code

VB
Imports System.Threading
Imports System.Net.Sockets

Public Class Form1

    'Socket para la escucha de peticiones
    Private oInitSocket As TcpListener

    Private nMensajes As Integer = 1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        'Inicializamos el socket de escucha
        oInitSocket = New TcpListener(Net.IPAddress.Any, 60000)

        'Lo ponemos a la escucha
        oInitSocket.Start(10)

        Do

            'Si hay conexiones pendientes
            If oInitSocket.Pending = True Then

                'Tomamos la conexión
                Dim oClientSocket As Socket = oInitSocket.AcceptSocket()

                'Recibimos los datos. Límite de buffer: 256
                Dim vDatos(255) As Byte

                oClientSocket.Receive(vDatos)

                'Los traducimos a texto y los mostramos
                'MsgBox("Mensaje del cliente: " & vbCrLf & System.Text.Encoding.BigEndianUnicode.GetString(vDatos))
                TextBox1.Text &= vbCrLf & "Mensaje nº " & nMensajes & ": " & _
                                        System.Text.Encoding.BigEndianUnicode.GetString(vDatos)

                'Desconectamos del cliente
                oClientSocket.Disconnect(False)

                'Cerramos el socket
                oClientSocket.Close()

                'Contamos un mensaje más recibido
                nMensajes += 1

                'Forzamos a pintar el formulario, ya que esta "bloqueado" trabajando en este bucle y no se pinta.
                TextBox1.Invalidate()

                Me.Refresh()

            End If

        Loop

    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.KeyChar = ""
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class
Posted
Updated 19-Jun-14 6:46am
v2
Comments
Sergey Alexandrovich Kryukov 19-Jun-14 12:55pm    
What do you call "not permanent connection". Not even FTP session? It would be a bad idea, because you would need to create equivalent of session, but by yourself. Without a session, even the delivery is not guaranteed (and retry is required), and the order of packages is also not guaranteed. With TCP, the problem is pretty easy.

Besides, you need to abstract UI from communication and other aspects. Don't mix text boxes and sockets.

You are hard-coding IP address. Are you serious? :-)

—SA
Deadlifer 19-Jun-14 20:03pm    
Yes sorry, I'm really newbie, I'm not exactly a programmer, but the thing is that I need a solution for that problem, I'm not doing the work alone but I want to add this visual improvement. with not permanent connection I want to say exactly what the images explains, there will be just a few mesages, but the box must close before that, and the next box have to move, I think i'm ok with the code by now. Is there any way to do that? Thanks for your answer Sir Sergey

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900