Solved

unix talk code ...

Posted on 2002-04-30
4
233 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 2

Expert Comment

by:pheur
ID: 6983122
0
 
LVL 3

Accepted Solution

by:
cincin77 earned 400 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 162
Coverting 24 hour time to 12 hour in C++ 15 178
Read file descriptor 3 in C 14 113
How to build c program using make in mingw environment? 9 72
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

830 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