Skip to content

Commit f265349

Browse files
committed
First commit of Unix socket chat application
0 parents  commit f265349

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

GPL-v2-license.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
This program is free software; you can redistribute it and/or modify
2+
it under the terms of the GNU General Public License as published by
3+
the Free Software Foundation; either version 2 of the License, or
4+
(at your option) any later version.
5+
6+
This program is distributed in the hope that it will be useful,
7+
but WITHOUT ANY WARRANTY; without even the implied warranty of
8+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9+
GNU General Public License for more details.
10+
11+
For the complete terms of the GNU General Public License, please see this URL:
12+
http://www.gnu.org/licenses/gpl-2.0.html

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Author:
2+
3+
- Bipul Islam
4+
5+
---
6+
##Unix Socet IPC Implementation
7+
8+
We use socket programming for inter process communications to design a simple console based chat application.
9+
10+
###Contents:
11+
- chatserv.c (Contains server code, on execution sets up the server to listen for connections)
12+
- charcli.c (Contains client code, set your server IP in the code it will connect to the server, use 127.0.0.1 to test on localhost)
13+
14+
###Usage:
15+
```
16+
gcc chatserv.c -o serv
17+
gcc chatcli.c -o cli
18+
./serv
19+
```
20+
(in a new tab)
21+
```
22+
./cli
23+
```
24+
Then chat away!
25+
26+
#####Notes:
27+
*It's possible to generalize both server and client by accepting the ip of the machines in command line, or even supply server ip to client in commadline.*

chatcli.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdio.h>
2+
#include <sys/types.h>
3+
#include <sys/socket.h>
4+
#include <netinet/in.h>
5+
#include <string.h>
6+
7+
main()
8+
{
9+
10+
struct sockaddr_in servaddr, myaddr;
11+
struct sockaddr_in client;
12+
int sockid;
13+
int connectid;
14+
int newsockid;
15+
int clientlen;
16+
17+
char msg_snd[100];//="Indian Statistical Institute";
18+
char msg_rcv[100];
19+
int sendid,recvid;
20+
int port_id = 6000;
21+
22+
23+
sockid = socket(AF_INET, SOCK_STREAM, 0);
24+
25+
bzero((char*)&servaddr, sizeof(struct sockaddr_in));
26+
servaddr.sin_family = AF_INET;
27+
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");// 192.168.23.44
28+
servaddr.sin_port = htons(port_id);
29+
30+
connectid = connect(sockid, (struct sockaddr*)&servaddr, sizeof(struct sockaddr_in));
31+
if(connectid < 0)
32+
printf("error connecting \n");
33+
while(1){
34+
35+
printf("(you)Guest::\n");
36+
37+
gets(msg_snd);
38+
sendid = sendto(sockid, msg_snd, 100, 0, (struct sockaddr*)&servaddr, sizeof(struct sockaddr_in));
39+
if(sendid < 0)
40+
printf("error sending\n");
41+
42+
recvid = recvfrom(sockid, msg_rcv, 100, 0, (struct sockaddr*)&servaddr, &clientlen);
43+
if(recvid < 0)
44+
printf("error recieving\n");
45+
46+
printf("server:: %s \n", msg_rcv);
47+
48+
}
49+
return 0;
50+
}

chatserv.c

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <stdio.h>
2+
#include <sys/types.h>
3+
#include <sys/socket.h>
4+
#include <netinet/in.h>
5+
#include <string.h>
6+
7+
main()
8+
{
9+
10+
struct sockaddr_in myaddr;
11+
struct sockaddr_in client;
12+
int sockid,sendid;
13+
int bindid;
14+
int newsockid;
15+
int clientlen;
16+
17+
char msg_rcv[100];
18+
char msg_snd[100];
19+
int recvid;
20+
int port_id = 6000;
21+
22+
23+
sockid = socket(AF_INET, SOCK_STREAM, 0);
24+
25+
bzero((char*)&myaddr, sizeof(struct sockaddr));
26+
myaddr.sin_family = AF_INET;
27+
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
28+
myaddr.sin_port = htons(port_id);
29+
30+
bindid = bind(sockid, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in));
31+
if(bindid < 0)
32+
printf("error \n");
33+
34+
listen(sockid, 5);
35+
clientlen = sizeof(struct sockaddr_in);
36+
37+
newsockid = accept(sockid, (struct sockaddr*)&myaddr, &clientlen);
38+
if(newsockid < 0)
39+
printf("error accepting\n");
40+
41+
while(1) {
42+
43+
printf("(you)Server::\n");
44+
recvid = recvfrom(newsockid, msg_rcv, 100, 0, (struct sockaddr*)&myaddr, &clientlen);
45+
if(recvid < 0)
46+
printf("error recieving\n");
47+
48+
printf("Guest:: %s \n", msg_rcv);
49+
gets(msg_snd);
50+
sendid = sendto(newsockid, msg_snd, 100, 0, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in));
51+
if(sendid < 0)
52+
printf("error Sending\n");
53+
54+
55+
56+
}
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)