[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

unix talk code ...

Posted on 2002-04-30
4
Medium Priority
?
239 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

607 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