Buchempfehlung
Windows-Programmierung. Das Entwicklerhandbuch zur WIN32-API
Windows-Programmierung. Das Entwicklerhandbuch zur WIN32-API
"Der" Petzold, das über 1000 Seiten starke Standardwerk zum Win32-API - besonders nützlich u. a. bei der GUI-Programmierung in FreeBASIC! [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

Linux Socket-verbindung

Uploader:Mitgliedmax06
Datum/Zeit:08.07.2007 15:26:31

#include "includes.bi"
#include "otherfuncs.bi"

#define newline chr(13) + chr(10)

Declare Sub ReConnect()
Declare Sub doInit
Declare Sub doShutdown
Declare Sub SendText(sendbuffer As String)
Declare Sub JoinChannels()
Declare Sub Listener()
Declare Function resolveHost(Byref hostname As String) As Integer
Declare Function RecvText() As String

Dim Shared socket As socket
Dim Shared nick As String
Dim Shared pass As String
Dim Shared host As String
Dim Shared Channels(0 To 65536) As String
Dim Shared ip As Integer
Dim Shared sa As sockaddr_in
Dim Shared s As String
Dim Shared author As String
Dim Shared Message As String
Dim Shared Ende As Ubyte
Dim Shared saccess As Ubyte
Dim Shared admin As Ubyte
Dim Shared AdminPass As String
Dim Shared admins(0 To 255) As String
Dim Shared x As Integer
Dim Shared found As Integer
Dim Shared AdminN As String
Dim Shared LastAction As Integer
Dim Shared actionname As String
Dim Shared MsgType As String
Dim Shared Dest As String
Dim Shared Chan As String
Dim Shared User As String
Dim Shared Temp As String
Dim Shared StepX As Integer
Dim Shared Finished As Integer
Dim Shared C As String
Dim Shared Col As String
Dim Shared CNT As Integer
Dim Shared frames As Integer
Dim Shared NewUser As String
Dim Shared glact As Integer
Dim Shared stathost As String
Dim Shared destframes As Integer
Redim Shared Users(Ubound(Channels)-Lbound(Channels), 256) As String


doinit()
ReConnect()
Threadcreate(@Listener,0)


SendText("NOTICE " + SubStr(AdminN,"!n=",1) + " :" + Temp + NewLine)

        '' Put the Record into the Database
        message = replace(message, newline, "")
        message = replace(message, chr(34), """)
        message = replace(message, "
'", "'")
        message = replace(message, "<", "<")
        message = replace(message, ">", ">")



doshutdown()

'#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-'
Sub Listener()

    Do
        s = ""
        Do
            s += recvText()
        Loop Until Instr(s, newline)

        saccess = 1
        Do
            Sleep 5
        Loop Until saccess = 0

    Loop Until Inkey = Chr(255) + "k" Or ende = 1
    
End Sub

Sub ReConnect()
    
    If socket <> 0 Then
        closesocket( socket )
    End If
    
    ip = resolveHost( host )
    If( ip = 0 ) Then
        Print "
resolveHost(): invalid address"
        End 1
    End If
    
    socket = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
    If( socket = 0 ) Then
        Print "
openSocket(): Something went wrong"
        End 1
    End If
    
    sa.sin_port         = htons( 6667 )
    sa.sin_family       = AF_INET
    sa.sin_addr.S_addr  = ip
    
    If ( connect( socket, cast( PSOCKADDR, @sa ), Len( sa )) = SOCKET_ERROR ) Then
        Print "
connect(): Something went wrong"
        closesocket( socket )
        End 1
    End If
    
    SendText("
NICK " + NICK + NEWLINE + "USER " + NICK + " 0 0 *:" + NICK + NEWLINE)

    If pass <> "" Then sendtext("PRIVMSG NickServ :IDENTIFY " + pass + NEWLINE)
    
    SendText("
PRIVMSG nickserv :set unfiltered on" + NEWLINE)
    
End Sub

Sub SendText(sendbuffer As String)
    If( send( socket, sendBuffer, Len( sendBuffer ), 0 ) = SOCKET_ERROR ) Then
        Print "
send(): Something went wrong"
        closesocket( socket )
        End 1
    End If
End Sub

Function RecvText() As String
    Dim recvbuffer As Zstring * 2
    Dim bytes As Integer
    bytes = recv( socket, recvBuffer, 1, 0 )    
    recvbuffer[bytes] = 0
    Return RecvBuffer
End Function


Sub doInit
    #ifdef __FB_WIN32__
    '' init winsock
    Dim wsaData As WSAData
    
    If( WSAStartup( MAKEWORD( 1, 1 ), @wsaData ) <> 0 ) Then
        Print "
Error: WSAStartup failed"
        End 1
    End If
    #Endif
End Sub

Sub doShutdown
    #ifdef __FB_WIN32__
    '' quit winsock
    WSACleanup
    #Endif
End Sub

Function resolveHost ( Byref hostname As String ) As Integer

    Dim ia As in_addr
    Dim hostentry As hostent Ptr

    '' check if it's an ip address
    ia.S_addr = inet_addr( hostname )
    If ( ia.S_addr = INADDR_NONE ) Then

        '' if not, assume it's a name, resolve it
        hostentry = gethostbyname( hostname )
        If ( hostentry = 0 ) Then
            Exit Function
        End If

        Function = *cast( Integer Ptr, *hostentry->h_addr_list )

    Else

        '' just return the address
        Function = ia.S_addr
    End If
End Function