Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

unix talk code ...

Posted on 2002-04-30
4
Medium Priority
?
238 Views
Last Modified: 2010-04-15
hello c experts,

i want to re-use the code of talk command in *nix ... where can i find the code ...

or can anybody can sent me the code .. he he he ...

thank you  
0
Comment
Question by:scave
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
cincin77 earned 1600 total points
ID: 6983873
try my version:))
the program below acts as both the client and the server
I also wrote a multicast version for it
If you wish I can send it,too




******************************************
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <netdb.h>
#include <signal.h>

void handler(int signum);  

int main(int argc,char *argv[])
{
int sock_r,sock_s;
fd_set rfds,wfds;
struct sockaddr_in recv_local;
struct sockaddr_in recv_dest;
struct sockaddr_in send_dest;
struct timeval tv;
int ret_read,ret_write;
int bytes_sent,bytes_read;
char *msg,*rec_msg;
int LOCAL_PORT,REMOTE_PORT;
int addr_len;
int i;

msg=(char *)calloc(80,1);
if (msg==NULL)
     {
        printf("memory allocation error 1\n");
        exit(1);
        }
rec_msg=(char *)calloc(80,1);
if (rec_msg==NULL)  
     {
     printf("memory allocation error 2\n");
     exit(1);
     }
if ((argc<4)||(argc>4))
{
printf("programin kullanimi:%s local_port remote_port \
remote_mach\n",argv[0]);
exit(1);
}
else
{
#define DEST_IP argv[3]
LOCAL_PORT=atoi(argv[1]);
REMOTE_PORT=atoi(argv[2]);

addr_len=sizeof(struct sockaddr);
if ((sock_r=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("receiver_socket:");
exit(1);
}
fcntl(sock_r,F_SETFL,O_NONBLOCK);

recv_local.sin_family = AF_INET;
recv_local.sin_port = htons(LOCAL_PORT);
recv_local.sin_addr.s_addr = INADDR_ANY;
bzero(&(recv_local.sin_zero),8);
 
if (bind(sock_r,(struct sockaddr *)&recv_local,sizeof(struct \
sockaddr))==-1)
{
perror("bind:");
exit(1);
}

if ((sock_s=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("sender_socket:");
exit(1);
}
fcntl(sock_s,F_SETFL,O_NONBLOCK);

send_dest.sin_family = AF_INET;
send_dest.sin_port = htons(REMOTE_PORT);
send_dest.sin_addr.s_addr = inet_addr(DEST_IP);
bzero(&(send_dest.sin_zero),8);


FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_SET(0, &rfds);  
FD_SET(sock_r, &rfds);  
FD_SET(sock_r, &wfds);

for (;;)
{
tv.tv_sec=0;
tv.tv_usec=300000;
FD_SET(0, &rfds);
FD_SET(sock_r, &rfds);
FD_SET(sock_r, &wfds);  
ret_read=select(sock_r+1,&rfds,&wfds,NULL,&tv);
if (ret_read>0)
{
if (FD_ISSET(0,&rfds))
     {
     fgets(msg,80,stdin);
     /*scanf("%s",msg);*/    
     for (;;)
     {
     tv.tv_sec=0;
        tv.tv_usec=300000;
     /*ret_write=select(sock_r+1,NULL,&wfds,NULL,&tv);*/  
     if (FD_ISSET(sock_r,&wfds))
          {
          /*printf("yolluyo\n");*/
          /*SOKETTEN BAS*/
          if ((bytes_sent=sendto(sock_r,msg,strlen(msg),0,(struct \
               sockaddr *)&send_dest,sizeof(struct sockaddr))) \
               == -1)
               {
               perror("sendto");
               exit(1);
               }
          for (i=0;i<80;i++)
                     *(msg+i)='\0';
          /*BUFFERI SIL*/
          break;
          }
     }/*infinite for*/
     }
if (FD_ISSET(sock_r,&rfds))
     {
     if ((bytes_read=recvfrom(sock_r,rec_msg,79,0,(struct
     sockaddr *)&send_dest,&addr_len)) == -1)
          {
          perror("recvfrom");
          exit(1);
          }
     printf("%s says: \
          %s\n",inet_ntoa(send_dest.sin_addr.s_addr),rec_msg);
     for (i=0;i<80;i++)
          *(rec_msg+i)='\0';
     /*bufferi sil*/
     }
}/*ret_read>0*/

}/* infinite for */

}/*argc check*/
return 1;
}
0
 

Author Comment

by:scave
ID: 7025888
i succeed compiling and running the code above ... but i want to send / receive files through above code (talk) ... i still cannot do it ... can somebody help me ...
0
 

Author Comment

by:scave
ID: 7564327
thank you very much ...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question