Visual Basic.NET - Ayuda con Sockets y comunicion TCP/IP

 
Vista:
sin imagen de perfil

Ayuda con Sockets y comunicion TCP/IP

Publicado por Jaime (23 intervenciones) el 03/10/2014 22:47:47
Hola a todos de nuevo estoy por aqui con un problema
estoy tratando de realizar una conexion entre algunos equipos por medio de un socket

pero cuando envio el equipo se cicla ese es mi primer error
el segundo es que cuando cierro el formulario que recibe y lo abro de nuevo este se pone muy lento

este es el codigo para enviar:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Net.Sockets
Imports System.Net
Imports System.Diagnostics
 
Public Class Socket_IP
    Dim Conexion As New Conexion()
    Public Sub Send_Message()
        Dim dts As New DataSet
        Dim strHostName As String
        Dim strIPAddress As String
        'cone.Delete("delete from IPS")
        strHostName = System.Net.Dns.GetHostName()
        strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
        Conexion.Seleccionar("SELECT * FROM IPS", dts)
        For Each row In dts.Tables(0).Rows
            If Not IsDBNull(row(0)) Then
                Dim ipaddres As String = row(0).ToString
                Socket_Send(ipaddres)
            End If
        Next
    End Sub
    Private Sub Socket_Send(ByVal IPaAddress As String)
        Dim Socket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Try
            Socket.Connect(IPaAddress, 8888)
        Catch ex As Exception
            'Lalalalala
        End Try
        Dim message As String = "Pedido"
        Dim buffer As Byte() = Encoding.ASCII.GetBytes(message)
        Try
            Socket.Send(buffer)
        Catch ex As Exception
            'lalalalala
        End Try
        Socket.Close()
    End Sub
End Class

Con este otro estoy recibiendo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Imports System
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Diagnostics
Imports System.Net.Sockets
 
Public Class Reu
    Dim h1, h2 As Thread, mensaje As String
    Dim CallLlenaGrid As New MethodInvoker(AddressOf Me.DataGridH2)
    Dim cone As New Conexion(), data, data2, data3 As New DataSet
    Dim dt1, dt2, dt3 As DataTable
    Dim con As Integer
    Dim number As String
    Dim fecha As Date = Today.Date
    Dim nombre As String
    Private Sub Form15_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''cone.Seleccionar("SELECT * FROM Ordenes", data2)
        ''dt2 = data2.Tables(0)
        ''Conse.DataSource = dt2
        ''con = Conse.Item(0, 0).Value
        Declarar()
        h1.Start()
        h2.Start()
    End Sub
    Private Sub Declarar()
        h1 = New Thread(AddressOf Me.Start_H1)
        h2 = New Thread(AddressOf Me.Start_H2)
    End Sub
    Private Sub Start_H1()
        Escuchar()
        If mensaje <> "" Then
            'h2.Suspend()
            If Not h2.ThreadState = Threading.ThreadState.Running Then
                h2.Resume()
            End If
            ''MessageBox.Show(mensaje)
            mensaje = ""
            Start_H1()
        Else
            Start_H1()
        End If
    End Sub
    Private Sub Escuchar()
        Dim Socket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Try
            Socket.Bind(New IPEndPoint(IPAddress.Any, 8888))
            Socket.Listen(1)
            Dim Client As Socket
            Client = Socket.Accept
            Dim buffer() As Byte = New Byte(20) {}
            Dim length As Integer
            Client.Receive(buffer)
            length = buffer.Length
            If length > 0 Then
                mensaje = Encoding.ASCII.GetString(buffer, 0, length)
            End If
        Catch ex As SocketException
            'MessageBox.Show("Error de Lectura() en: " + ex.ToString)
        End Try
        Socket.Close()
    End Sub
    Private Sub Start_H2()
        Cargar_Grid()
        Start_H2()
    End Sub
    Private Sub Cargar_Grid()
        Dim dts As New DataSet
        cone.Seleccionar("SELECT * FROM ORDENES", dts)
        data2 = dts
        Me.BeginInvoke(CallLlenaGrid)
        h2.Suspend()
    End Sub
    Private Sub DataGridH2()
        Conse.DataSource = data2.Tables(0)
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

y asi es como mando llamar el metodo

1
2
Dim socket_ip As New Socket_IP()
            socket_ip.Send_Message()

Bueno el sistema comprende 2 secciones una de ellas el usuario consume un producto y crea una orden de trabajo que se almacena en una base de datos

La segunda seccion se encuentra en manofactura y tiene que recibir la orden en cuanto se genere y mostrar en pantalla el numero de orden para que se comienze a producir

lo que yo quiero hacer es que cuando se genere la orden se envie un mensaje a la seccion 2 basicamente se refresque el datagrid y muestre un msgbox

Espero me pudieran apoyar con estos problemas

nunca antes habia manejado sockets
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder