Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,330 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,813 people online right now. Registration is fast and FREE... Join Now!




TCP chat client server

 
Closed TopicStart new topic

TCP chat client server

priyashe
1 Sep, 2008 - 02:59 AM
Post #1

D.I.C Head
**

Joined: 28 Aug, 2008
Posts: 71

I had done an application which is chat application using TCP sockets in which ip address of the server machine is given and once it is connected the data's can be sent to the machine for which ip address is provided and i need the data's which we sending to store it in a text file...is it possible and i attached the vb code below

CODE


tcp client

Option Explicit

' operator wants to exit the program
Private Sub cmdExit_Click()
    tcpClient.Close
    End
End Sub

' operator wants to send data to the server
Private Sub cmdSendString_Click()
    tcpClient.SendData txtSendData.Text
End Sub

' initialize variables
Private Sub Form_Load()
tcpClient.RemoteHost = "localhost"
tcpClient.RemotePort = 1091 'connect to echo server from Simple TCP/IP Services available on NT
tcpClient.LocalPort = 0  'pick a port that is in use... 80 is IIS (http)
tcpClient.Close
    txtRemoteIpAddress = "61.12.7.217"
    txtRemotePort = "81"
    txtSocketStatus = SocketStatus(tcpClient.State)
    txtLocalPort = tcpClient.LocalPort
    txtLocalIpAddress = tcpClient.LocalIP
    txtLocalHostName = tcpClient.LocalHostName
    tcpClient.Connect
End Sub
'Private Sub tcpClient_error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
' CancelDisplay = True
  'MsgBox "Error"
'End Sub


' operator wants to connect to the server
Private Sub cmdConnect_Click()
    If (tcpClient.State = sckClosed) Then
        ' Invoke the Connect method to initiate a connection.
        tcpClient.RemotePort = txtRemotePort
        tcpClient.RemoteHost = txtRemoteIpAddress
        tcpClient.Connect
        On Error Resume Next
tcpClient.Connect
If Err Then
   ' handle the error here
End If

        txtRemoteHostName = tcpClient.RemoteHost
    Else
        MsgBox "ERROR: socket is not closed.  Socket must be closed before opening a new connection"
    End If
End Sub

' the server disconnected the connection
Private Sub tcpClient_Close()
    tcpClient.Close
End Sub

' the server sent us data
Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String
    
    tcpClient.GetData strData, vbString
    txtOutput.Text = strData
End Sub

' winsock control encountered an error


' this timer simply updates the status of the socket control on the display
Private Sub Timer1_Timer()
    txtSocketStatus = SocketStatus(tcpClient.State)
    ' IF the socket is stuck in the closing or error states THEN close the socket to reset it
    If ((tcpClient.State = sckClosing) Or (tcpClient.State = sckError)) Then
        tcpClient.Close
    End If
End Sub




CODE


tcp server

Option Explicit

' operator wants to send message to remote computer
Private Sub cmdSendString_Click()
'tcpServer.SendData
    tcpServer.SendData txtSendData.Text
End Sub

' operator wants to exit this program
Private Sub cmdExit_Click()
    tcpServer.Close
    End
End Sub

'initialize variables
Private Sub Form_Load()
    tcpServer.Protocol = sckTCPProtocol
    tcpServer.LocalPort = 0
    tcpServer.Listen
    txtSocketStatus = SocketStatus(tcpServer.State)
    txtLocalPort = tcpServer.LocalPort
    txtLocalIpAddress = tcpServer.LocalIP
    txtLocalHostName = tcpServer.LocalHostName
End Sub


' remote client is closing the connection so we need to reset our end of the connection
Private Sub tcpServer_Close()
    tcpServer.Close
    tcpServer.Listen
End Sub

' This routine is called when a remote client asks to connect to this server
Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
' Check if the control's State is closed. If not,
' close the connection before accepting the new connection.
    If (tcpServer.State <> sckClosed) Then
        tcpServer.Close
    End If
' Accept the request with the requestID parameter.
    tcpServer.Accept requestID
    
End Sub

' This routine is called when the client sends data to the server
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)  ' Declare a variable for the incoming data.
    Dim strData As String
    
    tcpServer.GetData strData
    txtOutput.Text = strData
End Sub

' This routine is called when the Winsock control encounters an error
Private Sub tcpServer_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
   Debug.Print "Server Error Number = " + Str$(Number) + ",  Description = " + Description
   txtSocketStatus = Description
End Sub

' This timer routine is used to update the displayed status of the Winsock control
Private Sub Timer1_Timer()
    txtSocketStatus = SocketStatus(tcpServer.State)
    ' If the winsock control is stuck in the closing or error state then let's reset the winsock control
    If ((tcpServer.State = sckClosing) Or (tcpServer.State = sckError)) Then
        tcpServer.Close
        tcpServer.Listen
    End If
End Sub



User is offlineProfile CardPM
+Quote Post

priyashe
RE: TCP Chat Client Server
1 Sep, 2008 - 03:27 AM
Post #2

D.I.C Head
**

Joined: 28 Aug, 2008
Posts: 71

i have attached the code fully kindly help


Attached File(s)
Attached File  New_Folder.zip ( 18.34k ) Number of downloads: 95
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 12/2/08 07:49AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month