hello!
We are a work group and we are developing a practice of communication with sockets. The questions are: is it neccessary two sockets for client and server each one for transmit and receive information?? which is the code??
Or someone know some web page where do I find developed applications client-server with sockets??
The codes are:
client
.486 ; Crea codigo de 32 bits
.model flat, stdcall ; 32 bit Modelo de memoria
option casemap :none ; case sensitive
include \masm32\P\client.inc ; Archivo con Librerias
; ------------------
; Segmento de Datos
; ------------------
.data
Port dd 4000
.data?
s1 SOCKET ?
s2 SOCKET ?
sin1 sockaddr_in <>
sin2 sockaddr_in <>
wsaData WSADATA <>
txtinput DWORD ?
temp dd ?
hostt dd ?
AdressIP DWORD ?
HostAddr db 256 dup(?)
RecBuff db 512 dup(?)
EnvBuff db 512 dup(?)
edit dd ?
; ------------------------------
; Declaración de procedimientos
;-------------------------------
main PROTO
inicializar PROTO
envia PROTO
salir PROTO
;--------------------------------
.code
start:
call main
exit
;Procedimiento Principal
;-----------------------
main proc
call inicializar
main endp
;-----------------------
;Procedimiento para inicializar controladores y sockect´s
;-------------------------------------------------------
inicializar proc
cls
print chr$("--------------------------------------------------",13,10)
print chr$(" ",13,10)
print chr$(" Inicio PARQUEO 1.0 - UdeM ",13,10)
print chr$(" Para terminar pulse CTRL + C Simultaneamente ",13,10)
print chr$("--------------------------------------------------",13,10)
;Inicializa el DLL WindSock v1.1
invoke WSAStartup,0101h,ADDR wsaData
invoke socket,AF_INET,SOCK_STREAM,0
mov s1,eax
mov ax,AF_INET
mov sin1.sin_family,ax
invoke htons,Port
mov sin1.sin_port,ax
mov AdressIP, input("IP:")
invoke inet_addr, addr AdressIP
cmp eax,INADDR_NONE
je verporhost
mov sin1.sin_addr,eax
jne conexion
verporhost:
invoke gethostbyname,ADDR HostAddr
push eax
cmp eax,0
je errorIP2
pop eax
mov eax,(hostent ptr [eax]).h_list
mov eax,[eax]
mov eax,[eax]
mov sin1.sin_addr,eax
jne conexion
conexion:
print chr$(" ",13,10)
invoke socket,AF_INET,SOCK_STREAM,0
mov s2,eax
invoke connect, s2, addr sin1, sizeof sin1
cmp eax,SOCKET_ERROR
je conexion
call envia
errorIP2:
print chr$("Error al resolver direccion IP",13,10)
call inicializar
inicializar endp
;------------------------------------------------------
;Procedimiento para enviar al servidor
;-------------------------------------
envia proc
print chr$("Conectado.",13,10)
print chr$("--------------------------------------------------",13,10)
jne siguenv
siguenv:
print chr$(" ")
print chr$(" ",13,10)
print chr$(" ",13,10)
; Se imprime el titulo del programa y las opciones para escoger
print chr$(" -------------------------------------------------------------",13,10)
print chr$(" - UNIVERSIDAD DE MEDELLIN -",13,10)
print chr$(" - ARQUITECTURA DEL COMPUTADOR -",13,10)
print chr$(" - JUAN CARLOS BOTERO CUARTAS - DANILO GONZALEZ ARIAS -",13,10)
print chr$(" - -",13,10)
print chr$(" - Programa para controlar ingreso al parqueadero -",13,10)
print chr$(" - -",13,10)
print chr$(" -------------------------------------------------------------",13,10)
print chr$(" ",13,10) ; impresion de lineas en blanco
print chr$(" ",13,10)
print chr$(" ",13,10)
print chr$(" -------------------------------------------------------------",13,10)
print chr$(" - ESCOJA LA OPCION QUE DESEA: -",13,10)
print chr$(" - -",13,10)
print chr$(" - 1. INGRESAR PLACA -",13,10)
print chr$(" - 2. CONSULTAR PLACA -",13,10)
print chr$(" - 3. GENERAR FACTURA -",13,10)
print chr$(" - SALIR (CTRL + C) -",13,10)
print chr$(" - -",13,10)
print chr$(" -------------------------------------------------------------",13,10)
mov txtinput,input(" DIGITE LA OPCION: ")
invoke lstrlen, txtinput
mov temp, eax
invoke send,[s2],txtinput,temp,0
cmp eax,SOCKET_ERROR
je errors
lea ecx,[txtinput]
mov BYTE PTR [eax+ecx],0
jne siguenv1
siguenv1:
print chr$(" ")
print chr$(" ",13,10)
print chr$(" ",13,10)
print chr$("-------------------------------------------------------------",13,10)
mov txtinput,input("DIGITE LA PLACA: ")
invoke lstrlen, txtinput
mov temp, eax
invoke send,[s2],txtinput,temp,0
cmp eax,SOCKET_ERROR
je errors
lea ecx,[txtinput]
mov BYTE PTR [eax+ecx],0
jne siguenv1
errors:
print chr$("Envio fallo. Servidor probablemente desconectado.",13,10)
call salir
envia endp
;----------------------------------------------------------------------------
;------------------------------------
salir proc
invoke closesocket,s1
invoke closesocket,s2
invoke WSACleanup
xor eax,eax
call inicializar
salir endp
;-------------------------------------
end start
----------------------------------------------------------
server:
.486 ; Crea codigo de 32 bits
.model flat, stdcall ; 32 bit Modelo de memoria
option casemap :none ; case sensitive
include \masm32\P\client.inc ; Archivo con Librerias
; ----------------------------------------------------
; Segmento de Datos
; ----------------------------------------------------
.data
Port dd 4000
.data?
s1 SOCKET ?
s2 SOCKET ?
s3 SOCKET ?
sin1 sockaddr_in <>
sin2 sockaddr_in <>
sin3 sockaddr_in <>
wsaData WSADATA <>
txtinput dd ?
temp dd ?
hostt dd ?
AdressIPt dd ?
edit db 512 dup(?)
; ------------------------------
; Declaración de procedimientos
;-------------------------------
main PROTO
inicializar PROTO
ini PROTO
recibir PROTO
salir PROTO
.code
start:
call main
exit
;Procedimiento Principal
;-----------------------
main proc
call inicializar
main endp
;-----------------------
;Procedimiento para inicializar el dll del winsock y el listen del servidor
;--------------------------------------------------------------------------
inicializar proc
cls
print chr$("--------------------------------------------------",13,10)
print chr$(" ",13,10)
print chr$(" Inicio PARQUEO 1.0 - UdeM ",13,10)
print chr$(" Para terminar pulse CTRL + C Simultaneamente ",13,10)
print chr$("--------------------------------------------------",13,10)
print chr$("Esperando Conexion...",13,10)
;Inicializa el DLL WindSock v1.1
invoke WSAStartup,0101h,ADDR wsaData
invoke socket,AF_INET,SOCK_STREAM,0
mov s1,eax
mov ax,AF_INET
mov sin1.sin_family,ax
invoke htons,Port
mov sin1.sin_port,ax
mov eax,INADDR_ANY
mov sin1.sin_addr,eax
invoke bind,s1,ADDR sin1,SIZEOF sockaddr_in
cmp eax,SOCKET_ERROR
jne @F
invoke WSACleanup
xor eax,eax
ret
@@:
invoke listen,s1,1
conexion:
mov eax,SIZEOF sockaddr_in
mov temp,eax
invoke accept,s1,ADDR sin2,ADDR temp
cmp eax,INVALID_SOCKET
je conexion
call ini
inicializar endp
;---------------------------------------------------------------------
;Procedimiento para verificar el socket de comunicacion con el cliente
;---------------------------------------------------------------------
ini proc
mov s2,eax
print chr$("Conectado.",13,10)
print chr$("--------------------------------------------------",13,10)
cmp eax,SOCKET_ERROR
je errors
call recibir
errors:
print chr$("Envio fallo. Cliente probablemente desconectado.",13,10)
cls
call inicializar
ini endp
;-----------------------------------------------------------------------
;Procedimiento para recibir lo enviado por el cliente
;----------------------------------------------------
recibir proc
print chr$("Transmision")
print chr$(" ")
jmp siguere
siguere:
invoke recv,[s2], addr edit,SIZEOF edit,0
cmp eax,SOCKET_ERROR
lea ecx,[edit]
mov BYTE PTR [eax+ecx],0
je errorecibe
print offset edit
print chr$(" "),13,10
jmp siguere
errorecibe:
print chr$("Recepcion fallo.",13,10)
call salir
recibir endp
;------------------------------------------------------
;Procedimiento para cerrar la conexion
;-------------------------------------
salir proc
invoke closesocket,s1
invoke closesocket,s2
invoke WSACleanup
xor eax,eax
call inicializar
salir endp
;-------------------------------------
end start
Thank you!!
It is not necessary to use a socket for the send and a socket for the receive however there can be an a few advantages
in doing this. Try reading Effective Tcp/ip for details.
juancabot,
I have attached a sample client-server application. First run SampleServer and then each time you run SampleClient the server responses by showing a message. For more information this link http://tangentsoft.net/wskfaq/ would be useful. To get better result, try to ask specifically what you need. The source code you have sent is ambiguous and hard to understand. Also, Striker is a professional expert in networking. He have solved many problems of mine in networking. Ask him what you need to know about networking.
Regards,
Danesh
[attachment deleted by admin]