Crea sito

Archivio per la categoria network code

How to nat a PC with an other PC

Here is the situation: you have 2 (or more) PCs with Linux system. One PC has at least 2 network interface and with one on them it is connected on Internet. The second PC instead has no access to the internet. If you want that also the second PC gets a connection, you can use […]

Client TCP with PHP

Here there is a simple example of tcp client written in php that tries to connect to a server, sends to it 2 messages and prints the server replies <?php /* Get the port for the WWW service. */ $service_port = getservbyname(‘www’, ‘tcp’); /*if you already know the destination port, you don’t need getservbyname, you can […]

Tags: , , ,

Server TCP with PHP

Here there is a simple example of tcp server written in php that accepts a connection from a client and for each message that the client sends to the server, it reply with the same message. <?php /* Allow the script to hang around waiting for connections. */ set_time_limit(0); /* Turn on implicit output flushing […]

Tags: , , ,

Received tcp packets from a socket (C language)

Here there is an example of a simple tcp receiver (server) #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #define SOCKET_ERROR   ((int)-1) #define SIZEBUF 1000000 void usage(void) {  printf (“usage: servTCP LOCAL_PORT_NUMBER\n”); exit(1); } int main(int argc, char *argv[]) { #define MAXSIZE 1000000 struct sockaddr_in […]

Tags: , , ,

Send tcp packet over socket (C language)

Here there is an example of a simple tcp sender #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> #define SOCKET_ERROR   ((int)-1) #define SIZEBUF 1000000 void usage(void) {  printf (“usage: cliTCP REMOTE_IP_NUMBER REMOTE_PORT_NUMBER\n”); exit(1); } int main(int argc, char *argv[]) { #define MAXSIZE 1000000 struct sockaddr_in […]

Tags: , , ,

Received an UDP stream with a socket (C language)

Here there is an example of how read a stream udp received from the network using a socket #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <errno.h> #include <stdio.h> #include <arpa/inet.h> #define SOCKET_ERROR   ((int)-1) #define SIZEBUF 100000L void usage(void) {  printf (“parameters: LOCAL_PORT_NUMBER, IP ADDRESS (the latter is not necessary)\n”); exit(1); } int main(int argc, char […]

Tags: , , ,

Send udp stream over a socket (C language)

Here there is an example of a simple program that use a socket to send datagram UDP on the network #include<stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <errno.h> #define SOCKET_ERROR   ((int)-1) #define SIZEBUF 100000L int main(int argc, char * argv[]){ char  ip_dest[100]; char  ip_source[100]; short int port_dest, port_source; int socketfd, OptVal, addr_size; int ris, […]

Tags: , , ,