evo ga...
Code:
//server
#include "myheader.h" /* for user-defined constants */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> /* basic system data types */
#include <sys/socket.h> /* basic socket definitions */
#include <errno.h> /* for the EINTR constant */
#include <sys/wait.h> /* for the waitpid() system call */
#include <sys/un.h> /* for Unix domain sockets */
/*
The use of this functions avoids the generation of
"zombie" processes.
*/
void sig_chld( int signo )
{
pid_t pid;
int stat;
while ( ( pid = waitpid( -1, &stat, WNOHANG ) ) > 0 ) {
printf( "Child %d terminated.\n", pid );
}
}
int main( int argc, char **argv )
{
int listenfd; /* Socket descriptors. */
char line;
pid_t childpid;
socklen_t clilen;
struct sockaddr_un cliaddr, servaddr; /* Structs for the client and server socket addresses. */
signal( SIGCHLD, sig_chld ); /* Avoid "zombie" process generation. */
listenfd = socket( AF_LOCAL, SOCK_DGRAM, 0 ); /* Create the server's endpoint */
/* ATTENTION!!! THIS ACTUALLY REMOVES A FILE FROM YOUR HARD DRIVE!!! */
unlink( UNIXSTR_PATH ); /* Remove any previous socket with the same filename. */
bzero( &servaddr, sizeof( servaddr ) ); /* Zero all fields of servaddr. */
servaddr.sun_family = AF_LOCAL; /* Socket type is local (Unix Domain). */
strcpy( servaddr.sun_path, UNIXSTR_PATH ); /* Define the name of this socket. */
/* Create the file for the socket and register it as a socket. */
bind( listenfd, ( struct sockaddr* ) &servaddr, sizeof( servaddr ) );
char buf[100];
clilen = sizeof( cliaddr );
int bytes_no = recvfrom(listenfd, buf, 100, 0, (struct sockaddr*)&cliaddr, &clilen);
buf[bytes_no] = '\0';
if (bytes_no >0) {
bzero( &cliaddr, sizeof( cliaddr ) ); /* Zero all fields of servaddr. */
cliaddr.sun_family = AF_LOCAL; /* Socket type is local (Unix Domain). */
strcpy( cliaddr.sun_path, "client" ); /* Define the name of this socket. */
printf("rcvd. %s\n",buf);
if ((bytes_no = sendto(listenfd, buf, bytes_no, 0,
(struct sockaddr *)&cliaddr, sizeof(struct sockaddr))) == -1) {
printf ("errno=%d", errno);
}
memset(buf, 0, sizeof(buf));
}
}
Code:
//client
#include "myheader.h" /* for user-defined constants */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h> /* basic socket definitions */
#include <sys/types.h> /* basic system data types */
#include <sys/un.h> /* for Unix domain sockets */
#include <iostream>
using namespace std;
int main( int argc, char **argv )
{
int sockfd;
struct sockaddr_un servaddr, cliaddr; /* Struct for the server socket address. */
int pid;
char send;
char buf[100];
printf( "Write freely. Terminate with q.\n" );
sockfd = socket( AF_LOCAL, SOCK_DGRAM, 0 ); /* Create the client's endpoint. */
unlink( "client" ); /* Remove any previous socket with the same filename. */
bzero( &servaddr, sizeof( servaddr ) ); /* Zero all fields of servaddr. */
servaddr.sun_family = AF_LOCAL; /* Socket type is local (Unix Domain). */
strcpy( servaddr.sun_path, UNIXSTR_PATH ); /* Define the name of this socket. */
int listenfd = socket( AF_LOCAL, SOCK_DGRAM, 0 ); /* Create the server's endpoint */
bzero( &cliaddr, sizeof( cliaddr ) ); /* Zero all fields of servaddr. */
cliaddr.sun_family = AF_LOCAL; /* Socket type is local (Unix Domain). */
strcpy( cliaddr.sun_path, "client" ); /* Define the name of this socket. */
/* Create the file for the socket and register it as a socket. */
bind( listenfd, ( struct sockaddr* ) &cliaddr, sizeof( cliaddr ) );
int sent=0;
std::string strSend;
cin >> strSend;
if ((sent = sendto(sockfd, strSend.c_str(), strSend.size(), 0,
(struct sockaddr *)&servaddr, sizeof(struct sockaddr))) == -1) {
perror("sendto");
exit(1);
}
socklen_t clilen = sizeof( servaddr );
int rcvd = recvfrom(listenfd, buf, 100, 0, (struct sockaddr*)&servaddr, &clilen);
buf[rcvd] = '\0';
printf("server responded \"%s\"\n",buf);
close( sockfd );
}
znaci problem je sto udp soketi nisu konektovani i NE MOZES da saljes jednostavno samo onome od koga si primio jer te taj NE SLUSA... tako da i na strani klijenta moras imati primati poruke na nekom (drugom, mislim da moze i na istom, ali ne bih to radio

) portu ako zelis da dobijas odgovore sa servera.ovo je jednostavan primer i treba ga doterati,al ja za to nemam vremena
pozdrav
nikola
[Ovu poruku je menjao nikoladsp dana 01.08.2006. u 13:40 GMT+1]
ja sam panker sa diplomom kod moje mame...