IP Code
Posted: Fri Jan 15, 2010 12:04 am
I'm making small chat client for a few friends and was wondering if there is a way of getting their IP's without them having to type it manually?
Code: Select all
Structure CmdP_STARTUPINFO
cb.l
lpReserved.l
lpDesktop.l
lpTitle.l
dwX.l
dwY.l
dwXSize.l
dwYSize.l
dwXCountChars.l
dwYCountChars.l
dwFillAttribute.l
dwFlags.l
wShowWindow.w
cbReserved2.w
lpReserved2.l
hStdInput.l
hStdOutput.l
hStdError.l
EndStructure
Global proc.PROCESS_INFORMATION, start.CmdP_STARTUPINFO, sa.SECURITY_ATTRIBUTES, hReadP.l, hWriteP.l, lngBytesread.l, strBuff.s=Space(256)
Procedure.s Rd_cmd(comnd_send.s)
sa\nLength =SizeOf(SECURITY_ATTRIBUTES)
sa\bInheritHandle = 1
sa\lpSecurityDescriptor = 0
ret = CreatePipe_(@hReadP, @hWriteP, @sa, 0)
If ret = 0
MessageRequester("Information", "CreateP failed. Error: ",0)
End
EndIf
start\cb = SizeOf(CmdP_STARTUPINFO)
start\dwFlags = #STARTF_USESHOWWINDOW | #STARTF_USESTDHANDLES
start\hStdOutput = hWriteP
start\hStdError = hWriteP
ret = CreateProcess_(0, comnd_send, sa, sa, 1, #NORMAL_PRIORITY_CLASS, 0, 0, @start, @proc)
If ret <> 1
MessageRequester("Information","File Or command not found", 0)
End
EndIf
ret = CloseHandle_(hWriteP)
mOutputs.s = ""
While ret<>0
ret = ReadFile_(hReadP, strBuff, 255, @lngBytesread, 0)
If lngBytesread>0
mOutputs = mOutputs + Left(strBuff, lngBytesread)
EndIf
Wend
ret = CloseHandle_(proc\hProcess)
ret = CloseHandle_(proc\hThread)
ret = CloseHandle_(hReadP)
ProcedureReturn mOutputs
EndProcedure
If CreateFile(0, "IP.txt")
WriteString(0, Rd_cmd("ipconfig"))
CloseFile(0)
EndIf
Delay(10)
RunProgram("IP.txt")
Code: Select all
InitNetwork()
UserIPViewer$="http://h3x0r.ath.cx/Sonstiges/ShowMyIp12.php"
UserIP$ = StringField(GetHTTPHeader(UserIPViewer$), 2, Chr(34))
Debug UserIP$I understand; I figured that was probably the case.blueznl wrote:Your PC can not know its public IP adress if it is behind a NAT, as it cannot know how the nat translates the adress. The only option is to find a public machine on the internet which returns the IP adress to your program.
Well, Nituvious is getting his from an external source which means a dependance on the external source to tell you what you want to know. I haven't tried that with my code but its an idea and a good question.srod wrote:@SFSxOI : nice code. However that is returning the internal IP address assigned by my router and not the external one. Any way of modifying your code? Nituvious' code gives the correct external IP address.
Code: Select all
replace the line:
WriteString(0, Rd_cmd("ipconfig"))
with
WriteString(0, Rd_cmd("route print"))
Nope, not at all.SFSxOI wrote:you will get a whole bunch of info but one of those i think will be your external IP address
Code: Select all
Declare.s GetURL(mUrl.s)
Procedure.s GetURL(mUrl.s)
Static bonline.i,gServerID.i
Protected mHost.s,mFile.s,Header.s,sOut.s,*buffer
*buffer = AllocateMemory(1200)
i = FindString(mUrl,"/",1)
If i
mFile = Right(mUrl,Len(mUrl)-i)
mHost = Left(mUrl,i-1)
Else
mHost = mUrl
mfile = mUrl
EndIf
If bOnline = 0
gServerID = OpenNetworkConnection(mHost,80)
If Not gServerID
MessageRequester("Network Problem","Can't establish connection")
EndIf
EndIf
Timeout = ElapsedMilliseconds() + 5000
If gServerID
Header = "GET /"+ mFile + " HTTP/1.1"+#CRLF$
Header + "Host: "+ mHost + #CRLF$+#CRLF$
SendNetworkData(gServerID,@Header,Len(Header))
Repeat
Delay(2)
Until NetworkClientEvent(gServerID) = #PB_NetworkEvent_Data And ElapsedMilliseconds() < Timeout
If ElapsedMilliseconds() < Timeout
Repeat
lt = ReceiveNetworkData(gServerID,*Buffer,1200)
sout + PeekS(*Buffer,lt)
Until DataLength = 0
EndIf
pos = FindString(Sout,#CRLF$ + #CRLF$,1)
If pos <> 0
pos+3
sout = Right(Sout,Len(sout)-pos)
EndIf
CloseNetworkConnection(gServerID)
EndIf
FreeMemory(*Buffer)
ProcedureReturn sOut
EndProcedure
InitNetwork()
Debug GetUrl("ipecho.net/plain.php")
Code: Select all
<?php
echo $_SERVER['HTTP_REFERER'];
?>
Code: Select all
If pos <> 0
pos+3 ; < changed from 4 to 3
sout = Right(Sout,Len(sout)-pos)
EndIf