Server Client wsock32

Für allgemeine Fragen zur Programmierung mit PureBasic.
Benutzeravatar
MASH
Beiträge: 130
Registriert: 22.05.2005 17:43

Server Client wsock32

Beitrag von MASH »

Ich habe ein Problem mit dem Socket der klappt nur in eine Richtung und zwar vom Client zum Server und ich weiß nicht woran es liegt. Der Server sollte einfach nur ein Echo zurückgeben also die ursprüngliche Antwort.

Kann da mal wer drüber schauen?

Die Include

Code: Alles auswählen

;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;
; Include for wsock32.lib
;
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;{ Imports: wsock32.lib
;- Imports: wsock32.lib
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Import "wsock32.lib"
	
	;lib use
	WSAStartup(wVersionRequested.w,lpWSAData)
	WSACleanup()
	
	;stuff
	htons(hostshort)
	inet_ntoa(addr)		;inet_ntoa (struct in_addr in)
	;inet_aton(addr,adda)		;inet_ntoa (struct in_addr in)
	inet_addr(cp)
	gethostbyname(name)
	
	;srv
	bind(s,name,namelen)
	listen(s,backlog)
	accept(s,addr,addrlen)
	
	;client
	connect(s,name,namelen)
 	
 	;both sides
 	socket(af,type,protocol)
 	closesocket(s)		;close(c)
	recv(s,buf,len,flags)
	send(s,buf,len,flags)
EndImport
;}
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;{ Macros
;- Macros
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Macro mMakeWord(HiByte,LoByte)
	((LoByte & $FF) | ((HiByte & $FF) << 8))
EndMacro

Macro mHiByte(Word)
	((Word >> 8) & $FF)
EndMacro

Macro mLoByte(Word)
	(Word & $FF)
EndMacro
;}
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;{ Procedures
;- Procedures
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Procedure WSock_Init(HiVersion,LoVersion)
	Define.w Version
	Define.WSADATA SockData
	Define.c HiWord,LoWord
	Define ProcReturn
	
	Version = mMakeWord(HiVersion,LoVersion)
	
	If WSAStartup(Version,@SockData) <> #SOCKET_ERROR
		ProcReturn = #True
; 		With SockData
; 			If (mHiByte(\wVersion) = HiVersion)
; 				If (mLoByte(\wVersion) = LoVersion)
; 					
; 				EndIf
; 			EndIf
; 		EndWith
	EndIf
	
	ProcedureReturn ProcReturn
EndProcedure
;}
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Der Client

Code: Alles auswählen

IncludeFile "wsock32.pbi"


EnableExplicit

#Buffer_Size = $1000

#WSAStartup_Version_LO = 2
#WSAStartup_Version_HI = 2

Define hSocket,Connection
Define *Buffer,BytesReceived,SendedBytes,BoolReceived
Define.SOCKADDR_IN SocketSpec
Define.s String,Server = "127.0.0.1"	;localhost
Define Port = 7000

;use InitNetwork()
If WSock_Init(#WSAStartup_Version_HI,#WSAStartup_Version_LO)	;init network lib
	hSocket = socket(#PF_INET,#SOCK_STREAM,#IPPROTO_TCP)
	If hSocket <> #SOCKET_ERROR
		Debug "Connecting to: " + Server + ":" + Str(Port)
		
		With SocketSpec
			\sin_port = htons(Port)
			\sin_family = #AF_INET
			\sin_addr = inet_addr(@Server)	;m_MakeIPString(Server)
		EndWith
		
		Connection = connect(hSocket,@SocketSpec,SizeOf(SOCKADDR_IN))
		If Connection <> #SOCKET_ERROR
			Debug "Connected to server"
			*Buffer = AllocateMemory(#Buffer_Size)		;memset zero
			If *Buffer
				Debug "Buffer prepared"
				String = "HELLO 1"
				PokeS(*Buffer,String)
				SendedBytes = send(hSocket,*Buffer,Len(String),0)
				If SendedBytes <> #SOCKET_ERROR
					Debug "Send bytes: " + Str(SendedBytes)
					*Buffer = ReAllocateMemory(*Buffer,SendedBytes)		;memset zero
					If *Buffer
						Debug "Buffer reprepared"
						Repeat
							BytesReceived = recv(Connection,*Buffer,SendedBytes,0)
							If BytesReceived > 0
								Debug BytesReceived
								Debug "Get bytes: " + Str(BytesReceived)
								Debug PeekS(*Buffer,BytesReceived)
							EndIf
						ForEver
					EndIf
				Else
					Debug "send() failed"
				EndIf
				FreeMemory(*Buffer)
			EndIf
			
	    closesocket(hSocket)
		Else
			Debug "connect() failed"
		EndIf
	Else
		Debug "socket() failed"
	EndIf
	WSACleanup()
EndIf


End 0

Der Server

Code: Alles auswählen

IncludeFile "wsock32.pbi"


EnableExplicit

#Buffer_Size = $1000

Define hSocket,Connection,SocketClientSize
Define *Buffer,BytesReceived,BytesTransmitted,BoolReceived
Define.SOCKADDR_IN SocketSpec,SocketClient
;Define.SOCKADDR SocketClient

Define Port = 7000

#WSAStartup_Version_LO = 2
#WSAStartup_Version_HI = 2


InitNetwork()
If WSAStartup(#WSAStartup_Version_HI,#WSAStartup_Version_LO)
	hSocket = socket(#PF_INET,#SOCK_STREAM,#IPPROTO_TCP)	; #IPPROTO_IP = 0
	If hSocket <> #SOCKET_ERROR	; #SOCKET_ERROR = -1
		With SocketSpec
			\sin_addr = #INADDR_ANY
			\sin_port = htons(Port)
			\sin_family = #AF_INET
		EndWith
		
		If bind(hSocket,@SocketSpec,SizeOf(SOCKADDR_IN)) <> #SOCKET_ERROR
			If listen(hSocket,#SOMAXCONN) <> #SOCKET_ERROR
				Repeat
					SocketClientSize = SizeOf(SOCKADDR_IN)
					Connection = accept(hSocket,SocketClient,@SocketClientSize)
					If Connection <> #SOCKET_ERROR
						If SocketClientSize > 0
							Debug "Client from: " + PeekS(inet_ntoa(SocketClient\sin_addr))
						Else
							Debug "Unkonwn client!"
						EndIf
						*Buffer = AllocateMemory(#Buffer_Size)		;memset zero
						If *Buffer
							Repeat
								;Flags: #MSG_PEEK, #MSG_OOB, #MSG_DONTROUTE, #MSG_MAXIOVLEN
								BytesReceived = recv(Connection,*Buffer,#Buffer_Size,0)
								If BytesReceived > 0
									Debug "Message from client: " + PeekS(*Buffer,BytesReceived)
									BytesTransmitted = send(Connection,*Buffer,BytesReceived,0)
									If BytesTransmitted <> #SOCKET_ERROR
										Debug "Backtransmission: " + Str(BytesTransmitted)
										BoolReceived = #True
									EndIf
								Else
									BoolReceived = #True
								EndIf
							Until BoolReceived
							FreeMemory(*Buffer)
						EndIf
	       	Else
	       		;Debug "accept() failed"
					EndIf
				ForEver
			Else
				Debug "listen() failed"
			EndIf
		Else
			Debug "bind() failed"
		EndIf
	Else
		Debug "socket() failed"
	EndIf
Else
	Debug "WSAStartup() failed"
EndIf

End 0

Danke Mark S.