* 구성요소에서 Microsoft Winsock Control 6.0(SP6)를 추가해줍니다
종료 밑 컴퓨터 모양이 Winsock Control
* 구성요소 추가하는데 Type Library위치가 잘못되면 오류가 뜰 수 있다
' Ip, Port창을 placehorder와 같이 커서가 올라가면 Text창 안의 문구를 지워줌
Dim msText As String
Dim nsText As String
' 닫기 버튼
Private Sub ExitBtn_Click()
Me.Hide
End Sub
Private Sub Form_Load()
msText = IpTxt.Text
nsText = PortTxt.Text
End Sub
' 텍스트박스에 포커스가 가면 빈칸으로 변경
Private Sub IpTxt_GotFocus()
IpTxt.Text = ""
End Sub
Private Sub PortTxt_GotFocus()
PortTxt.Text = ""
End Sub
' 텍스트 박스에서 포커스가 벗어나면 기존 문구 출력
Private Sub IpTxt_LostFocus()
If IpTxt.Text = "" Then
IpTxt.Text = msText
End If
End Sub
Private Sub PortTxt_LostFocus()
If PortTxt.Text = "" Then
PortTxt.Text = nsText
End If
End Sub
Private Sub ConnectBtn_Click()
On Error GoTo err
' 서버가 닫혀 있으면
If Winsock1.State = 0 Then
ConnectBtn.Caption = "Send"
Winsock1.Protocol = sckTCPProtocol ' TCP Protocol : 전송 제어 프로토콜
Winsock1.RemoteHost = IpTxt.Text ' Ip입력
Winsock1.RemotePort = CLng(PortTxt.Text) ' Port입력, Clng: long형으로 변환
Winsock1.Connect ' 연결
' 서버 열려 있으면
ElseIf Winsock1.State = 7 Then
' Winsock1.SendData Text1.Text ' Text1에 적힌 내용을 전송
'' List1.AddItem "Send : " & Date & " " & Time & " " & Text1.Text
'' Text1.Text = ""
AddList ("")
End If
err:
If err <> 0 Then
Call MsgBox(err.Description)
End If
End Sub
Private Sub DisConnectBtn_Click()
On Error GoTo err
' 서버가 열려있으면
If Winsock1.State = 7 Then
Winsock1.Close
ConnectBtn.Caption = "Connect"
' 서버가 닫혀 있으면
ElseIf Winsock1.State = 0 Then
MsgBox "서버가 닫혀있습니다", vbOKOnly, "알림"
List1.AddItem "접속이 종료되었습니다"
ConnectBtn.Caption = "연결"
End If
err:
If err <> 0 Then
Call MsgBox(err.Description)
End If
End Sub
' Server 연결 끊김
Private Sub Winsock1_Close()
On Error GoTo err
Dim i%
Dim a As Variant
ConnectBtn.Caption = "서버 연결 끊김"
Label1.Caption = "서버 동작 중지"
For i = 1 To 1
If i = 1 Then
Label2.Caption = "서버연결이 끊겼습니다. 클라이언트를 재시작해주세요"
End If
Exit For
Next
err:
If err <> 0 Then
Call MsgBox(err.Description)
End If
End Sub
' Server로부터 Accept가 되면 발생되는 이벤트
Private Sub Winsock1_Connect()
List1.AddItem "접속되었습니다"
Label1.Caption = "서버 동작 중"
End Sub
' Client로부터 데이터 수신이되면 발생
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
On Error GoTo err
Dim buf$
Winsock1.GetData buf
If Trim(buf) = "[close]" Then
Winsock1.Close
End If
List1.AddItem "Receive : " & Date & " " & Time & " " & buf
err:
If err <> 0 Then
Call MsgBox(err.Description)
End If
End Sub
' Text1에 내용 입력 후 엔터키 누를 시 서버로 데이터 전송
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo err
Dim strMsg$
If KeyCode = 13 Then ' 13이 엔터를 의미
AddList (strMsg)
End If
err:
If err <> 0 Then
Call MsgBox(err.Description)
End If
End Sub
Private Sub SendTxt(ByVal txt As String)
On Error GoTo err
txt = Trim(txt)
Winsock1.SendData txt
err:
If err <> 0 Then
Call MsgBox(err.Description)
End If
End Sub
' SendTxt를 공통으로 사용하기 위해서(Enter눌러서 보낼때와 Send버튼을 눌렀을때 동일 기능)
Private Sub AddList(AddText As String)
strMsg = Text1.Text
Call SendTxt(strMsg)
List1.AddItem "Send : " & Date & " " & Time & " " & strMsg
Text1.Text = ""
End Sub
Private Sub ServerDisConnect()
If Winsock1.State = 0 Then
ConnectBtn.Caption = "다시 연결"
Else
ConnectBtn.Caption = "연결 중"
End If
End Sub
'VB' 카테고리의 다른 글
[VB6.0] 아스키코드(ASCII) 변환 (0) | 2022.05.24 |
---|---|
[VB6.0] Socket Server(소켓 서버) (0) | 2022.05.18 |
[VB6.0] Serial 포트 (0) | 2022.05.16 |
[VB.NET] TreeView (0) | 2022.05.13 |
[VB.NET] ListBox / ListView (0) | 2022.05.13 |
댓글