Solved

Simple Question with Winsock

Posted on 2003-11-06
17
442 Views
Last Modified: 2013-11-13
ok.. i am just learning to use winsock and not very experienced with it, but i would like to learn.  what i want to do is get it to send info besides text over to another form.  what do i put in the data arrival to make it do stuff like: making a label visible/invisible on both forms? or changing the color to something.  thx...
0
Comment
Question by:learning_t0_pr0gram
  • 9
  • 8
17 Comments
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9698544
its a 2 person thingy that u connect to through IPs... i forgot to put that
0
 

Expert Comment

by:ecla
ID: 9698728
to make your winsock do stuff as you mentioned... you need to send the custom commands and then you'll have a checking on the other side to match the commands with their specific actions. For e.g.

first person issue this command:
winsock.send "Make Label Visible"

second person's winsock_dataarrival event:

select case senddata
case "Make Label Visible"
 label.visible = false
case "Forms Invisible"
 me.visible = false
end select
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9698925
ok, but i'm using an array...  like label2(index).visible = false

how do i send the "index" part so it knows which one to make invisible?
0
 

Expert Comment

by:ecla
ID: 9698982
you have the option of defining what the structure of the string would be,  it's just a matter of parsing the string...

Instead of sending only "Make Visible". You can send "MakeVisible[1]" where 1 is the index. You'll then modify your case statement so that it can accomodate the indexas  such:

dim i as integer
dim str as string

select case senddata
case "Make Label Visible"
str = replace(senddata, "Make Label Visible[", "")
str =  replace(senddata, "]", "")

 i = cint(str)
 label(i).visible = false
case "Forms Invisible"
 me.visible = false
end select

The point is... you can be as flexible as you wanna be.. becuase you edfine how the strings should be interpreted.
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9699044
it gives me an error on case senddata ..and i tried using the "incoming" text thing, and that just put "Make Label Visible[4]ÿÿ" in the chat box... how do i get it to read the non text?  do i use something besides senddata? ...sorry, i'm kinda stupid and i'm not familiar with winsock at all :(
0
 

Expert Comment

by:ecla
ID: 9699089
senddata, is a variable where the data received from winsock is stored...

You might wanna post your code here..
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9699120
what i'm trying to make is a tic tac toe game... i got the game to work WITHOUT the other player seeing it...

i am self taught and i have not been programming very long, so if the code is kind of scratchy, i'm sorry.. but here it is

Option Explicit
Dim x
Dim z As Integer

Sub Test(xo As Label)
If turns.Caption = "x" Then
xo.Caption = "X"
Else
xo.Caption = "O"
End If
End Sub

Private Sub Blah12_Click()
Call cmdClose_Click
End Sub

Private Sub cmdClose_Click()
cmdClose.Enabled = False
txtName.Enabled = True
cmdListen.Enabled = True
cmdConnect.Enabled = True
txtIn.text = "*** Disconnected" & vbCrLf
wsChat.Close
End Sub

Private Sub cmdConnect_Click()
If txtIP.text = "" Or txtName.text = "" Then
MsgBox "You must enter both an IP and Chat Name first", vbExclamation, "Error!"
txtName.SetFocus
Exit Sub
End If
On Error Resume Next

wsChat.Close
wsChat.Connect txtIP.text, 1235
cmdClose.Enabled = True
cmdListen.Enabled = False
cmdConnect.Enabled = False
txtName.Enabled = False
End Sub

Private Sub cmdListen_Click()
If txtName.text = "" Then
MsgBox "You must enter a Chat Name first", vbExclamation, "Error!"
txtName.SetFocus
Exit Sub
End If
txtIP.text = wsChat.LocalIP
wsChat.Close
wsChat.LocalPort = 1235
wsChat.Listen
cmdClose.Enabled = True
cmdListen.Enabled = False
cmdConnect.Enabled = False
txtName.Enabled = False
AddText "*** Waiting For Friend", txtIn
End Sub

Private Sub Command1_Click()
txtIP.text = wsChat.LocalIP
End Sub


Private Sub Command2_Click()
For x = 0 To 8
Label2(x).Caption = ""
Label2(x).Enabled = True
Next
loseFrame.Visible = False
L1.Visible = False
L2.Visible = False
L3.Visible = False
L4.Visible = False
L5.Visible = False
L6.Visible = False
L7.Visible = False
L8.Visible = False

End Sub

Private Sub Command3_Click()
Call cmdClose_Click
End Sub

Private Sub Command4_Click()
frameTie.Visible = False
For x = 0 To 8
Label2(x).Enabled = False
Next
End Sub

Private Sub Command5_Click()
loseFrame.Visible = False
For x = 0 To 8
Label2(x).Enabled = False
Next
End Sub

Private Sub Command6_Click()
player.Caption = "x"
introFrame.Visible = False
End Sub

Private Sub Command7_Click()
player.Caption = "o"
introFrame.Visible = False
End Sub

Private Sub Command8_Click()
winFrame.Visible = False
For x = 0 To 8
Label2(x).Enabled = False
Next
End Sub

Private Sub Label2_Click(Index As Integer)
z = 0
Test Label2(Index)
Label2(Index).Enabled = False
If turns.Caption = "x" Then
turns.Caption = "o"
Else
turns.Caption = "x"
End If
WinYesNoWithoutTie Label2(0), Label2(3), Label2(6), L5, winFrame, loseFrame, player
WinYesNoWithoutTie Label2(1), Label2(4), Label2(7), L4, winFrame, loseFrame, player
WinYesNoWithoutTie Label2(2), Label2(5), Label2(8), L3, winFrame, loseFrame, player
WinYesNoWithoutTie Label2(0), Label2(4), Label2(8), L2, winFrame, loseFrame, player
WinYesNoWithoutTie Label2(2), Label2(4), Label2(6), L1, winFrame, loseFrame, player
WinYesNoWithoutTie Label2(0), Label2(1), Label2(2), L8, winFrame, loseFrame, player
WinYesNoWithoutTie Label2(3), Label2(4), Label2(5), L7, winFrame, loseFrame, player
WinYesNoWithoutTie Label2(6), Label2(7), Label2(8), L6, winFrame, loseFrame, player
' that is in a module... i'm sure i could do it taking up less room, but i do what i can
For x = 0 To 8
If Label2(x).Enabled = False Then
z = z + 1
End If
Next
If z = 9 Then
frameTie.Visible = True
Exit Sub
End If
End Sub

Private Sub playAgain1_Click()
For x = 0 To 8
Label2(x).Caption = ""
Label2(x).Enabled = True
Next
frameTie.Visible = False
L1.Visible = False
L2.Visible = False
L3.Visible = False
L4.Visible = False
L5.Visible = False
L6.Visible = False
L7.Visible = False
L8.Visible = False

End Sub

Private Sub playAgain3_Click()
For x = 0 To 8
Label2(x).Caption = ""
Label2(x).Enabled = True
Next
winFrame.Visible = False
L1.Visible = False
L2.Visible = False
L3.Visible = False
L4.Visible = False
L5.Visible = False
L6.Visible = False
L7.Visible = False
L8.Visible = False

End Sub

Private Sub txtOut_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Trim(txtOut.text) = "" Then Exit Sub
wsChat.SendData txtName.text & ": " & txtOut.text
AddText txtName.text & ": " & txtOut.text, txtIn
txtOut.text = ""
txtOut.SetFocus
End If
End Sub

Private Sub wsChat_Connect()
Do
DoEvents
Loop Until wsChat.State = sckConnected Or wsChat.State = sckError
If wsChat.State = sckConnected Then
AddText "*** Connected" & vbCrLf, txtIn
txtName.Enabled = False
txtOut.Enabled = True
txtOut.SetFocus
Else
AddText "*** Connection Failed" & vbCrLf, txtIn
End If
End Sub

Private Sub AddText(ByVal text As String, ByRef Box As TextBox)
Box.text = Box.text & text & vbCrLf
Box.SelStart = Len(Box.text)
End Sub

Private Sub wsChat_ConnectionRequest(ByVal requestID As Long)
wsChat.Close
wsChat.Accept requestID
AddText "*** Connected" & vbCrLf, txtIn
introFrame.Visible = True
txtName.Enabled = False
txtOut.Enabled = True
txtOut.SetFocus
End Sub

Private Sub wsChat_DataArrival(ByVal bytesTotal As Long)
Dim incoming As String
wsChat.GetData incoming
AddText incoming, txtIn
End Sub

Private Sub wsChat_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)
If Number <> 0 Then
AddText "*** Error  " & Description & vbCrLf, txtIn
Call cmdClose_Click
End If
End Sub


the frames are like.. there is a frame for if u win/lose/tie .....a different' frame will pop up with an option to play again, just chat, or disconnect

there is one of them on each frame
0
 

Expert Comment

by:ecla
ID: 9699130
Modify this code to check if data received is a command or a message:

Private Sub wsChat_DataArrival(ByVal bytesTotal As Long)
Dim incoming As String
wsChat.GetData incoming
AddText incoming, txtIn
End Sub

into:

Private Sub wsChat_DataArrival(ByVal bytesTotal As Long)
Dim incoming As String
wsChat.GetData incoming
dim i as integer
dim str as string

select case trim(incoming)
case "Make Label Visible"
str = replace(senddata, "Make Label Visible[", "")
str =  replace(senddata, "]", "")
 i = cint(str)
 label(i).visible = false
exit sub
case "Forms Invisible"
 me.visible = false
exit sub
end select

AddText incoming, txtIn
End Sub
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9699138
ok, so what would i put when u click on Label 2 to make it send the data that i clicked on it?

wschat.senddata "Make Label Visible[" & index * "]"    

would i put that?  .....i'm confusing myself
0
 

Expert Comment

by:ecla
ID: 9699156
Yup,

Sorry modify the procedure again:

Private Sub wsChat_DataArrival(ByVal bytesTotal As Long)
Dim incoming As String
wsChat.GetData incoming
dim i as integer
dim str as string

select case trim(incoming)
case "Make Label Visible"
str = replace(incoming, "Make Label Visible[", "")
str =  replace(incoming, "]", "")
 i = cint(str)
 label(i).visible = false
exit sub
case "Forms Invisible"
 me.visible = false
exit sub
end select

AddText incoming, txtIn
End Sub

0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9699167
ok, now it sends "Make Label Visible[4]" inside the chat box... thats what it was doing before, and why i posted this message :(
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9699173
here is my source if u wanna try it:
http://angelfire.com/creep/chrisblah/tic_tac_toe.zip
0
 

Expert Comment

by:ecla
ID: 9699176
did you use my modified code procedure?

where did you put your wskchat.senddata "Make Label Visible[4]"
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9699179
http://angelfire.com/creep/chrisblah/tic_tac_toe.zip

download it and look ....just make it a .exe and open 2 of them and u can connect to yourself ...then click in a spot on the game board and look
0
 

Accepted Solution

by:
ecla earned 50 total points
ID: 9699228
Private Sub wsChat_DataArrival(ByVal bytesTotal As Long)

Dim incoming As String
wsChat.GetData incoming
Dim i As Integer
Dim str As String

Select Case InStr(incoming, "Make Label Visible")
Case Is > 0
str = Replace(incoming, "Make Label Visible[", "")
str = Replace(str, "]", "")
 i = CInt(str)
 Label2(i).Visible = True
 
Exit Sub
End Select

AddText incoming, txtIn
end sub
0
 
LVL 4

Author Comment

by:learning_t0_pr0gram
ID: 9699249
ok, thanks, that works... now i can TRY to get it to do the rest.. doubt i can though... i still gotta make the X's and O's show on the other board and stuff... :(
0
 

Expert Comment

by:ecla
ID: 9699260
OK gud luck, don't forget to award the points
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now