Categories
програмиране софтуер

Linux Socket Programming – Echo client/server

Демонстрация на програмиране на сокети под Linux. В конкретния случай са реализирани програмните фрагменти на клиент и сървър:

server.c

#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<error.h>
#include<strings.h>
#include<unistd.h>
#include<arpa/inet.h>
#define MAX_CLIENTS 20
#define BUFFER 1024
main(int argc, char **argv)
{
struct sockaddr_in server, client;
int sock, connection, data_len, struct_len = sizeof(struct sockaddr_in);
char data[BUFFER];
if((sock = socket(AF_INET, SOCK_STREAM,0)) == -1)
{
perror("Socket error.");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[1]));
server.sin_addr.s_addr = INADDR_ANY;
bzero(&server.sin_zero,8);
if((bind(sock,(struct sockaddr *)&server,struct_len)) == -1)
{
perror("Bind error.");
exit(-1);
}
if((listen(sock,MAX_CLIENTS)) == -1)
{
perror("Listen error.");
exit(-1);
}
while(1)
{
pid_t pid = fork();
if((connection = accept(sock,(struct sockaddr *)&client,&struct_len)) == -1)
{
perror("Accept error.");
exit(-1);
}
data_len = 1;
while(data_len)
{
data_len = recv(connection, data, BUFFER, 0);
if(data_len)
{
send(connection, data, data_len, 0);
data[data_len] = '\0';
printf("%s",data);
}
}
close(connection);
}
close(sock);
}

client.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<strings.h>
#include<arpa/inet.h>
#define ERROR -1
#define BUFFER 1024
main(int argc, char **argv)
{
struct sockaddr_in server;
int sock, len;
char input[BUFFER], output[BUFFER],data[BUFFER];
char *whoami=getlogin();
if((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
{
perror("Socket error.");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);
bzero(&server.sin_zero, 8);
if((connect(sock,(struct sockaddr *)&server,
sizeof(struct sockaddr_in))) == ERROR)
{
perror("Connect error.");
exit(-1);
}
while(1)
{
fgets(input, BUFFER, stdin);
strcpy(data, whoami);
strcat(data, ": ");
strcat(data, input);
send(sock, data, strlen(data), 0);
len = recv(sock, output, BUFFER, 0);
output[len] = '\0';
printf("Echo: %s",output);
}
close(sock);
}

By Димитър Минчев

Доктор по информатика и компютърни науки

2 replies on “Linux Socket Programming – Echo client/server”

Примерен лог на изпълнение със закачени клиенти:

~ PuTTY log 2010.10.29 11:09:53 ~
login as: user1
user1@172.16.0.110‘s password:
Linux virt-server 2.6.18-6-amd64 #1 SMP Fri Dec 12 05:49:32 UTC 2008 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Fri Oct 29 09:15:38 2010 from 172.16.1.77
user1@virt-server:~$ ./tcp-server 5000
New client connected.
Echo: 08311019
Client terminate connection.
New client connected.
Client terminate connection.
New client connected.
Client terminate connection.
New client connected.
Echo: 08311032
Echo: 
Client terminate connection.
New client connected.
Echo: 08311030 & 08311004
Client terminate connection.
New client connected.
Echo: 08311021
New client connected.
Echo: 08311009
Client terminate connection.
New client connected.
Client terminate connection.
New client connected.
Echo: 08311051
New client connected.
Echo: 08311022
Client terminate connection.
New client connected.
Echo: 08311016
Client terminate connection.
New client connected.
Echo: 08311010
Echo: 08311080
Client terminate connection.
New client connected.
Echo: 08311031
Client terminate connection.
New client connected.
Echo: 08311014
Echo:
Client terminate connection.
New client connected.
Echo: 08311013
Client terminate connection.
New client connected.
Echo: 08311029
Client terminate connection.
New client connected.
Echo: 08311013
Client terminate connection.
New client connected.
Echo: 08311011
Client terminate connection.
New client connected.
Echo: 08311026
Client terminate connection.
New client connected.
Echo: 08311013
Client terminate connection.
New client connected.
Echo: 08311005
Client terminate connection.
New client connected.
Echo: ima li bira
Client terminate connection.
New client connected.
Echo: nqma bira
Client terminate connection.
New client connected.
Echo: nqma bira
Client terminate connection.
New client connected.
Echo: koi pita za bira be
Client terminate connection.
New client connected.
Echo: 08311031
Client terminate connection.
New client connected.
Echo: nz
Client terminate connection.
New client connected.
Echo: koi iska
Client terminate connection.
New client connected.
Echo: vsichki
Client terminate connection.
New client connected.
Echo: na nqkoi pushi li mu se ?
Client terminate connection.
New client connected.
Echo: nikoi
Client terminate connection.
New client connected.
Echo: ai pochivka
Client terminate connection.
New client connected.
Echo: jaden sym samo
Client terminate connection.
New client connected.
Echo: tishina
Echo: promenete programta da pozvolqva mnogo conec-cii
Client terminate connection.
New client connected.
Echo: wisky s fastaci
Client terminate connection.
New client connected.
Echo: A SEGA DE !!!!!
Client terminate connection.
New client connected.
Echo: xaxa
Client terminate connection.
New client connected.
Echo: Pozvolqvame mnogo konekcii
Client terminate connection.
New client connected.
Echo: [
Client terminate connection.
New client connected.
Echo: dsaasd
Client terminate connection.
New client connected.
Echo: total FAIL !
Client terminate connection.
New client connected.
Echo: Ko praym sq ??
Client terminate connection.
New client connected.
Echo: cccccccccccccccccccc
Client terminate connection.
New client connected.
Echo: qska isfiri neshto
Client terminate connection.
New client connected.
Echo: total mi se hodi na kafe
Client terminate connection.
New client connected.
Echo: wisky s fastaci
Echo: ai v pochivkata 6te se obsajda doma6nata po mreji
Echo: za pove4e kontakti tarsete razkloniteli ili simeonov
New client connected.
Client terminate connection.
New client connected.
Client terminate connection.
New client connected.
Client terminate connection.
New client connected.
Client terminate connection.
New client connected.
Echo: zzz
Client terminate connection.
New client connected.
Echo:
Echo:
Echo:
Client terminate connection.
New client connected.
Client terminate connection.
New client connected.
Client terminate connection.
New client connected.
Echo: sfghasdfha
Client terminate connection.
New client connected.
Echo: fglkfg
Echo: gfgf
Echo: 😀
Echo: jnjn
Echo: df
Client terminate connection.
New client connected.
Echo: probvame samo dali shte se izhvarli
Client terminate connection.
New client connected.
Echo: gfdg
Client terminate connection.
New client connected.
Echo: xaxa
Client terminate connection.
New client connected.
Echo: dedewc
Client terminate connection.
New client connected.
Echo: ioana 4erpi vsi4ki wisky
Client terminate connection.
New client connected.
Echo: dsadsa
Client terminate connection.
New client connected.
Echo: 08311013
Client terminate connection.
New client connected.
Echo: sad
Client terminate connection.
New client connected.
Echo: dadada
Client terminate connection.
New client connected.
Echo: kk
Client terminate connection.
New client connected.
Echo: Zdr!!!!
Client terminate connection.
New client connected.
Echo: 08311013
Client terminate connection.
New client connected.
Echo: jfdfds
Client terminate connection.
New client connected.
Echo: This will erase all you information on C: Are you sure Y/N ?
Client terminate connection.
New client connected.
Echo: test
Client terminate connection.
New client connected.
Echo: Ko praym sq ?
Client terminate connection.
New client connected.
Echo: test
Client terminate connection.
New client connected.
Echo: fdsfgsdgfds
Client terminate connection.
New client connected.

Echo: wisky s fastaci
Echo: ai v pochivkata 6te se obsajda doma6nata po mreji
Echo: za pove4e kontakti tarsete razkloniteli ili simeonov

eheheh tva me razbi :]

Comments are closed.