?
Solved

Creating a chat

Posted on 2003-03-11
5
Medium Priority
?
241 Views
Last Modified: 2013-11-13
i'm trying to create an online chat. i've created the following system: every 1 sec., my app creates a .txt file on the internet, and also opens it. the effect of that will be: the user type his message and click "send", then the app writes a file with what he typed. the app opens it and the other user will read his message. but with the updates, my textbox keeps returning to the first line, and anyone can read the messages. how can i corret this problem, or is there any other way of creating a chat app? please help!
0
Comment
Question by:HyMaX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:rjgrande
ID: 8116997
Sounds like a tedious way to go about this project... So far I've taken a slightly different approach. I've written two apps: a client and a server. The server will use the Winsock control and listen on a port of your chosing. The client app will connect to the server app also using the Winsock control.

Beyond this you can either create your own proprietary protocol (lots of tedious error checking, but possibilities to add more features) or just rely on plain text communications.

Advantages:
1) More flexibility
2) Ability to send and receive in real time

Disadvantages:
1) The server app needs to be outside of any firewalls (or behind a firewall where you can open up incoming connections on the port that your app uses)
2) You need to find some way for the client app to find the server app's IP address (possibilities include dynamic DNS services, or probably what you're currently doing to send the text to be used only as a lookup for the IP)

I guess what it comes down to is what you want to get out of your app.

If this didn't answer your question (or you would prefer to use your current approach), via what means are you sending this text file? Uploading it to an FTP? Let me know. Good luck on your project.
0
 

Author Comment

by:HyMaX
ID: 8120811
ok, i liked that! could you tell me how to work with this "winsock" thing? and yes, i was trying to upload a file via FTP to the web... but your way is better!
0
 
LVL 1

Accepted Solution

by:
rjgrande earned 200 total points
ID: 8121010
Excellent. Here we go. The comments should get you by. If you have any questions, just let me know:

'-------------------------
' Here's the server app:
'-------------------------
' On the form I've already drawn 2 textboxes named txtSend and txtDisplay
' txtDisplay is a locked multiline textbox with vertical scrollbars
' txtSend is a regular single line textbox
' A winsock control (Winsock1) has also been drawn

Option Explicit

Private Sub Form_Load()
  'lets have winsock1 listen for incoming connections
  Winsock1.LocalPort = 12358 'specify practically any port you would like. it's generally not
  Winsock1.Listen            'a good idea to use ports <1024 or well known trojan ports <g>
End Sub

Private Sub Form_Resize()
  txtDisplay.Width = Me.Width - 120 'all cosmetic here
  txtSend.Width = Me.Width - 120
End Sub

Private Sub txtSend_KeyPress(KeyAscii As Integer)
  'when you hit enter (chr(13)) in txt send, send the data if winsock1 is connected
  If KeyAscii = 13 Then
    If Winsock1.State = sckConnected Then
      Winsock1.SendData txtSend.Text
      txtDisplay.Text = txtDisplay.Text & vbCrLf & "Server> " & txtSend.Text
      txtDisplay.SelStart = Len(txtDisplay.Text)
      txtSend.Text = vbNullString
    End If
    KeyAscii = 0 'avoid having the app beep at us
  End If
End Sub

Private Sub Winsock1_Close()
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "*** Remote host closed connection"
  txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  'an incoming connection request was received
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "*** Connection request received; accepting"
  txtDisplay.SelStart = Len(txtDisplay.Text)
  Winsock1.Close 'get winsock1 out of its listening state so we can accept the connection
  Call Winsock1.Accept(requestID)
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  'get whatever text was received from the winsock control
  Dim sbuffer As String
  Call Winsock1.GetData(sbuffer)
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "Client> " & sbuffer
  txtDisplay.SelStart = Len(txtDisplay.Text) 'scroll to the bottom (entirely cosmetic)
End Sub

'-------------------------
' Here's the client app:
'-------------------------
' Same controls as server app
'
Option Explicit

Private Sub Form_Load()
  'change the ip address here to whatever the ip is on the server app
  'also make sure you are using the same port number to connect to on your server
  Call Winsock1.Connect("127.0.0.1", 12358)
End Sub

Private Sub Form_Resize()
  txtDisplay.Width = Me.Width - 120 'all cosmetic here
  txtSend.Width = Me.Width - 120
End Sub

Private Sub txtSend_KeyPress(KeyAscii As Integer)
  'when you hit enter (chr(13)) in txt send, send the data if winsock1 is connected
  If KeyAscii = 13 Then
    If Winsock1.State = sckConnected Then
      Winsock1.SendData txtSend.Text
      txtDisplay.Text = txtDisplay.Text & vbCrLf & "Client> " & txtSend.Text
      txtDisplay.SelStart = Len(txtDisplay.Text)
      txtSend.Text = vbNullString
    End If
    KeyAscii = 0 'avoid having the app beep at us
  End If
End Sub

Private Sub Winsock1_Close()
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "*** Remote host closed connection"
  txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub

Private Sub Winsock1_Connect()
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "*** Connected to remote host"
  txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  'get whatever text was received from the winsock control
  Dim sbuffer As String
  Call Winsock1.GetData(sbuffer)
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "Server> " & sbuffer
  txtDisplay.SelStart = Len(txtDisplay.Text) 'scroll to the bottom (entirely cosmetic)
End Sub

0
 
LVL 1

Expert Comment

by:rjgrande
ID: 8124871
Excellent. Here we go. The comments should get you by. If you have any questions, just let me know:

'-------------------------
' Here's the server app:
'-------------------------
' On the form I've already drawn 2 textboxes named txtSend and txtDisplay
' txtDisplay is a locked multiline textbox with vertical scrollbars
' txtSend is a regular single line textbox
' A winsock control (Winsock1) has also been drawn

Option Explicit

Private Sub Form_Load()
  'lets have winsock1 listen for incoming connections
  Winsock1.LocalPort = 12358 'specify practically any port you would like. it's generally not
  Winsock1.Listen            'a good idea to use ports <1024 or well known trojan ports <g>
End Sub

Private Sub Form_Resize()
  txtDisplay.Width = Me.Width - 120 'all cosmetic here
  txtSend.Width = Me.Width - 120
End Sub

Private Sub txtSend_KeyPress(KeyAscii As Integer)
  'when you hit enter (chr(13)) in txt send, send the data if winsock1 is connected
  If KeyAscii = 13 Then
    If Winsock1.State = sckConnected Then
      Winsock1.SendData txtSend.Text
      txtDisplay.Text = txtDisplay.Text & vbCrLf & "Server> " & txtSend.Text
      txtDisplay.SelStart = Len(txtDisplay.Text)
      txtSend.Text = vbNullString
    End If
    KeyAscii = 0 'avoid having the app beep at us
  End If
End Sub

Private Sub Winsock1_Close()
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "*** Remote host closed connection"
  txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  'an incoming connection request was received
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "*** Connection request received; accepting"
  txtDisplay.SelStart = Len(txtDisplay.Text)
  Winsock1.Close 'get winsock1 out of its listening state so we can accept the connection
  Call Winsock1.Accept(requestID)
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  'get whatever text was received from the winsock control
  Dim sbuffer As String
  Call Winsock1.GetData(sbuffer)
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "Client> " & sbuffer
  txtDisplay.SelStart = Len(txtDisplay.Text) 'scroll to the bottom (entirely cosmetic)
End Sub

'-------------------------
' Here's the client app:
'-------------------------
' Same controls as server app
'
Option Explicit

Private Sub Form_Load()
  'change the ip address here to whatever the ip is on the server app
  'also make sure you are using the same port number to connect to on your server
  Call Winsock1.Connect("127.0.0.1", 12358)
End Sub

Private Sub Form_Resize()
  txtDisplay.Width = Me.Width - 120 'all cosmetic here
  txtSend.Width = Me.Width - 120
End Sub

Private Sub txtSend_KeyPress(KeyAscii As Integer)
  'when you hit enter (chr(13)) in txt send, send the data if winsock1 is connected
  If KeyAscii = 13 Then
    If Winsock1.State = sckConnected Then
      Winsock1.SendData txtSend.Text
      txtDisplay.Text = txtDisplay.Text & vbCrLf & "Client> " & txtSend.Text
      txtDisplay.SelStart = Len(txtDisplay.Text)
      txtSend.Text = vbNullString
    End If
    KeyAscii = 0 'avoid having the app beep at us
  End If
End Sub

Private Sub Winsock1_Close()
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "*** Remote host closed connection"
  txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub

Private Sub Winsock1_Connect()
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "*** Connected to remote host"
  txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  'get whatever text was received from the winsock control
  Dim sbuffer As String
  Call Winsock1.GetData(sbuffer)
  txtDisplay.Text = txtDisplay.Text & vbCrLf & "Server> " & sbuffer
  txtDisplay.SelStart = Len(txtDisplay.Text) 'scroll to the bottom (entirely cosmetic)
End Sub

0
 

Author Comment

by:HyMaX
ID: 8129319
rjgrande, that was excellent!!! thank you very much, now i'm able to create a particular chat service for me and my frends! thanks!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question