TCP Server(ohne Blockingcall!) + Client
Übersicht

![]() |
VertexBetreff: TCP Server(ohne Blockingcall!) + Client |
![]() Antworten mit Zitat ![]() |
---|---|---|
Socket.bmax wie immer unter http://blitzbasic.com/codearcs...0#comments zu finden.
Server Code: [AUSKLAPPEN] Strict
Import "Socket.bmx" Local tWSA : TWSAData Local iServerSocket : Int Local tAddr : TSockAddr Local tAddrRemote : TSockAddr Local iRemoteLen : Int Local iResult : Int Local iClient : Int[10] Local iIndex : Int Local tFDSRead : TFD_SET Local tTimeout : TTimeval Local tBuffer : TBank Local sMessage : String Print "TCP Server" Print "" tWSA = New TWSAData Print "Starting Winsock..." If s_WSAStartup(MAKESHORT(2, 0), tWSA) <> 0 Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK ( "+tWSA.sDescription+" \ "+tWSA.sSystemStatus+" )" Print "" EndIf Print "Creating TCP-Socket..." iServerSocket = s_socket(AF_INET, SOCK_STREAM, 0) If iServerSocket = INVALID_SOCKET Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK" Print "" EndIf tAddr = New TSockAddr tAddr.shSinFamily = AF_INET tAddr.shSinPort = s_htons(1234) tAddr.iSinAddr = INADDR_ANY Print "Binding socket on port 1234..." iResult = s_bind(iServerSocket, tAddr, 16) If iResult = SOCKET_ERROR Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK" EndIf Print "Set socket in listen mode..." iResult = s_listen(iServerSocket, 10) If iResult = SOCKET_ERROR Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK" EndIf For iIndex = 0 To 9 iClient[iIndex] = INVALID_SOCKET Next tFDSRead = New TFD_SET tAddrRemote = New TSockAddr tTimeout = New TTimeval tTimeout.iTV_Sec = 1 tTimeout.iTV_USec = 0 tBuffer = CreateBank(256) While True s_FD_ZERO(tFDSRead) s_FD_SET(iServerSocket, tFDSRead) For iIndex = 0 To 9 If iClient[iIndex] <> INVALID_SOCKET Then s_FD_SET(iClient[iIndex], tFDSRead) EndIf Next iResult = s_select(0, tFDSRead, Null, Null, tTimeout) If iResult = SOCKET_ERROR Then Print "Error: "+s_WSAGetLastError() Print "" End If If s_FD_ISSET(iServerSocket, tFDSRead) Then For iIndex = 0 To 9 If iClient[iIndex] = INVALID_SOCKET Then iRemoteLen = 16 iClient[iIndex] = s_accept(iServerSocket, tAddrRemote, Varptr iRemoteLen) Print "New Client ( ID: "+iIndex+" ):" Print " Port: "+s_ntohs(tAddrRemote.shSinPort) Print " IP: "+s_inet_ntoa(tAddrRemote.iSinAddr) Print "" Exit EndIf Next EndIf For iIndex = 0 To 9 If iClient[iIndex] <> INVALID_SOCKET Then If s_FD_ISSET(iClient[iIndex], tFDSRead) Then iResult = s_recv(iClient[iIndex], BankBuf(tBuffer), 256, 0) If iResult = 0 Or iResult = SOCKET_ERROR Then s_closesocket(iClient[iIndex]) iClient[iIndex] = INVALID_SOCKET Print "Client ( ID:"+iIndex+" ) has disconnected" Print "" Else Print "Client ( ID:"+iIndex+" ):" Print " "+iResult+" Bytes received" Print " Message: "+"".FromCString(BankBuf(tBuffer)) Print "" Print "Sending message to Client ( ID:"+iIndex+" )..." sMessage = "I have received: "+"".FromCString(BankBuf(tBuffer)) iResult = s_send(iClient[iIndex], sMessage.ToCString(), Len(sMessage)+1, 0) If iResult = SOCKET_ERROR Then Print " Error: "+s_WSAGetLastError() Print "" Else Print " OK ( "+iResult+" Bytes sended )" Print "" EndIf EndIf EndIf EndIf Next Print "Drink a beer, or make other coole things!" Wend Print "Closing socket..." If s_closesocket(iServerSocket) = SOCKET_ERROR Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK" Print "" EndIf Print "Closing Winsock..." If s_WSACleanup() = SOCKET_ERROR Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK" Print "" EndIf Print "Good bye!" Delay 1000 End Client Code: [AUSKLAPPEN] Strict
Import "Socket.bmx" Local tWSA : TWSAData Local iSocket : Int Local tAddr : TSockAddr Local iResult : Int Local tBuffer : TBank Local sMessage : String Print "TCP Client" Print "" tWSA = New TWSAData Print "Starting Winsock..." If s_WSAStartup(MAKESHORT(2, 0), tWSA) <> 0 Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK ( "+tWSA.sDescription+" \ "+tWSA.sSystemStatus+" )" Print "" EndIf Print "Creating TCP-Socket..." iSocket = s_socket(AF_INET, SOCK_STREAM, 0) If iSocket = INVALID_SOCKET Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK" Print "" EndIf tAddr = New TSockAddr tAddr.shSinFamily = AF_INET tAddr.shSinPort = s_htons(1234) tAddr.iSinAddr = s_inet_addr("127.0.0.1") Print "Connecting socket to 127.0.0.1 ..." iResult = s_connect(iSocket, tAddr, 16) If iResult = SOCKET_ERROR Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK" Print "" EndIf tBuffer = CreateBank(256) While True sMessage = Input("Message: ") If sMessage = "quit" Then Exit Print " Sending message..." iResult = s_send(iSocket, sMessage.ToCString(), Len(sMessage)+1, 0) If iResult = SOCKET_ERROR Then Print " Error: "+s_WSAGetLastError() Print "" Else Print " OK ( "+iResult+" Bytes sended )" Print "" EndIf Print "Waiting For incomming data..." iResult = s_recv(iSocket, BankBuf(tBuffer), 256, 0) If iResult = SOCKET_ERROR Then Print " Error: "+s_WSAGetLastError() Print "" Else Print " OK ( "+iResult+" Bytes received )" Print " Message: "+"".FromCString(BankBuf(tBuffer)) Print "" EndIf Wend Print "Closing socket..." If s_closesocket(iSocket) = SOCKET_ERROR Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK" Print "" EndIf Print "Closing Winsock..." If s_WSACleanup() = SOCKET_ERROR Then Print " Error: "+s_WSAGetLastError() Delay 2000 ; End Else Print " OK" Print "" EndIf Print "Good bye!" Delay 1000 End mfg olli |
||
vertex.dreamfall.at | GitHub |
Übersicht


Powered by phpBB © 2001 - 2006, phpBB Group