Paul, a TCP SYN (Synchronize) packet needs a three-part process called Handshaking. These are:
1) Sending a starter package (SYN) from the client to the server
2) Sending a synchronization acknowledgment packet from the server to the client (SYN-ACK - Synchronize Acknowledge)
3) The end of the handshake in three parts, sent by the client to the server or recognition message (ACK - Acknowledge).
As the TCP protocol has several sub-protocols such as HTTP, this handshake varies a lot and to implement this it is necessary not only programming knowledge but knowledge of the protocol to which the handshake is being made
So I suggest that you study more about SYN packet exchange and the protocols you want to synchronize (or connect - or check ports).
Edited:
As promised, he follows an example of communication with 3-part authentication implementing only the idea, but no specific protocol.
As for the protocol I will abstain from the example because there are many possibilities and probably any example will not serve the author of the question, since he did not specify any.
Follows server and client code. To test execute on the same machine the server first and then after an instance of the client.
Server Code:
#! / usr / bin / python
import socket
import thread
class ServidorTcp:
def __init__(self, host, porta):
self.TAMANHO_BUFFER = 1024
self.socket_servidor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket_servidor.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket_servidor.bind((host, porta))
def escutar(self):
self.socket_servidor.listen(5)
print("Aguardando conexoes")
while 1:
(cliente, endereco) = self.socket_servidor.accept()
print("Cliente conectado: " + endereco[0])
thread_cliente = thread.start_new_thread(self.sincronizar, (cliente, endereco))
def sincronizar(self, cliente, endereco):
retorno = cliente.recv(self.TAMANHO_BUFFER)
if(retorno == "SYN"):
cliente.send("SYN-ACK")
retorno = cliente.recv(self.TAMANHO_BUFFER)
if(retorno == "ACK"):
print("Sincronizado com o cliente remoto.")
cliente.close()
#executar o server.
servidor = ServidorTcp('localhost', 7171)
servidor.escutar()
Client Code:
#!/usr/bin/python
import socket
class ClienteTcp:
def __init__(self):
self.TAMANHO_BUFFER = 1024
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def conectar(self, host, porta):
print("Conectando a " + host + ":" + str(porta) + "...")
self.socket.connect((host, porta))
def sincronizar(self):
self.socket.send("SYN")
retorno = self.socket.recv(self.TAMANHO_BUFFER)
autenticado = False
if retorno == "SYN-ACK":
self.socket.send("ACK")
#Conexao efetuada com sucesso.
autenticado = True
return autenticado
def enviar(self, mensagem):
self.socket.send(mensagem)
def fechar(self):
self.socket.close()
#executar o cliente.
cliente = ClienteTcp()
cliente.conectar('localhost', 7171)
if cliente.sincronizar():
#Agora sei com quem estou me comunicando e a conversa pode iniciar entre as pontas.
print("Sincronizado com o servidor remoto.")
else:
print("Nao foi possivel estabelecer o sincronismo com o servidor remoto.")
I hope my answer was helpful.