Socket Programming
VIDEOS:
Sockets Explained
UDP in Php
UDP in Assembly Language (Linux x64)
UDP in C (3 lines of code)
UDP
UDP server in PHP
<?php $s=socket_create(AF_INET,SOCK_DGRAM,0); //create a UDP socket socket_bind($s,'0.0.0.0',5555); //bind on all interfaces on port 5555 socket_recvfrom($s,$b,10,0,$client_ip,$client_port); //read max 10 bytes from socket $s into buffer $b echo "Received: ".$b." from IP=".$client_ip.":".$client_port; socket_sendto($s,"hello",5,0,$client_ip,$client_port); //send a message back to the client ?>
UDP client in PHP
<?php $s=socket_create(AF_INET,SOCK_DGRAM,0); //create a UDP socket socket_sendto($s,"hey",3,0,"127.0.0.1",5555); //send 3 bytes to the server socket_recvfrom($s,$b,10,0,$serv_ip,$serv_port); //read max 10 bytes from socket $s into buffer $b echo "Received: ".$b." from IP=".$serv_ip.":".$serv_port; ?>
UDP server in Python 2
import socket s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.bind(("0.0.0.0",5555)) buff,addr=s.recvfrom(10) print buff s.sendto("hello", addr)
UDP client in Python 2
import socket s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.sendto("hey",("127.0.0.1",5555)) print s.recvfrom(10)
UDP client in Python 3
import socket s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) msg="hey" s.sendto(str.encode(msg),("127.0.0.1",5555)) msg,adr=s.recvfrom(10) print (msg.decode())
UDP server in C
#include <netinet/ip.h> char buf[8] = "xxxxxxxx"; int sfd,r; struct sockaddr_in soc,csoc; main (){ int clen=sizeof(struct sockaddr_in); sfd = socket (AF_INET, SOCK_DGRAM, 0); soc.sin_family=AF_INET; soc.sin_port=htons(7777); soc.sin_addr.s_addr=inet_addr("0.0.0.0"); bind(sfd,&soc,sizeof(struct sockaddr_in)); r=recvfrom (sfd,buf,100,0,&csoc,&clen); buf[r]=0; printf(" %d %s\n",strlen(buf),buf); }
UDP client in C
#include <netinet/ip.h> char buf[8] = "hello"; int sfd; struct sockaddr_in soc; main (){ sfd=socket(AF_INET, SOCK_DGRAM, 0); soc.sin_family=AF_INET; soc.sin_port=htons(7777); soc.sin_addr.s_addr=inet_addr("127.0.0.1"); sendto (sfd,buf,strlen(buf),0,&soc,sizeof(struct sockaddr_in)); } }
UDP client in Java
import java.io.*; import java.net.*; class c { public static void main(String args[]) throws Exception { DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("localhost"); byte buf[] = new byte[5]; String s="12345"; buf=s.getBytes(); DatagramPacket sendPacket = new DatagramPacket(buf, 5, IPAddress, 5555); clientSocket.send(sendPacket); } }
TCP
TCP server in PHP
<?php $s=socket_create(AF_INET,SOCK_STREAM,0); socket_bind($s,"0.0.0.0",9999); socket_listen($s); $cs=socket_accept($s); socket_send($cs,"Hello",5,0); ?>
TCP client in PHP
<?php $s=socket_create(AF_INET,SOCK_STREAM,0); socket_connect($s,"127.0.0.1",9999); socket_recv($s,$buf,20,0); echo $buf."\n"; ?>
TCP server in Python 2
import socket s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind(("0.0.0.0",7777)) s.listen(5) cs,addr=s.accept() b=cs.recv(10) print b cs.send("Hello") cs.close()
TCP client in Python 2
import socket s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect(("127.0.0.1",7777)) s.send("Salut") print s.recv(10) s.close()
TCP multithreaded server in Python
import time import socket from threading import Thread def f(cs,i): print ("Procesez client"+str(i)) b=cs.recv(10) time.sleep(10) cs.send(str(i)) cs.close() s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind(("0.0.0.0",7777)) s.listen(5) i=0 while (1==1): i=i+1 cs,addr=s.accept() t=Thread(target=f,args=(cs,i,)) t.start()
Python Multiplexed Chat Server – using select
import select, socket s= socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setblocking(0) s.bind(('0.0.0.0', 5555)) s.listen(5) ins = [s] outs = [] msg=[] while ins: r,w,e = select.select(ins,outs,ins) for ss in r: if ss==s: cs,addr=ss.accept() print "Client from:",addr ins.append(cs) else: print "Data from client" data=ss.recv(100) if data: txt=ss.getpeername()[0]+":"+str(ss.getpeername()[1])+":"+data print txt msg.append(txt) if ss not in outs: outs.append(ss) else : print "Null data, client is gone" ins.remove(ss) if ss in outs: outs.remove(ss) ss.close() for ss in e: print "Error" ins.remove (ss) if ss in outs: outs.remove(ss) ss.close() for ss in w: for i in msg: ss.send(i) msg=[]
Python client for the Multiplexed Chat Server
import socket,os s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect(("127.0.0.1",5555)) s.send("Hello") if os.fork()==0: while 1: print s.recv(100) exit(0) while 1: s.send(raw_input()) s.close()