Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <netdb.h>
  3. #include <netinet/in.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <arpa/inet.h>
  10. #include <errno.h>
  11. #define MAX 100
  12. #define SA struct sockaddr
  13. #include <net/if.h>
  14. #include <sys/ioctl.h>
  15. //added for nonblock
  16. #define BACKLOG 10
  17. #include <fcntl.h>
  18.  
  19.  
  20.  
  21.  
  22. // Returns hostname for the local computer
  23. void checkHostName(int hostname)
  24. {
  25. if (hostname == -1)
  26. {
  27. perror("gethostname");
  28. exit(1);
  29. }
  30. }
  31.  
  32. // Returns host information corresponding to host name
  33. void checkHostEntry(struct hostent * hostentry)
  34. {
  35. if (hostentry == NULL)
  36. {
  37. perror("gethostbyname");
  38. exit(1);
  39. }
  40. }
  41.  
  42. // Converts space-delimited IPv4 addresses
  43. // to dotted-decimal format
  44. void checkIPbuffer(char *IPbuffer)
  45. {
  46. if (NULL == IPbuffer)
  47. {
  48. perror("inet_ntoa");
  49. exit(1);
  50. }
  51. }
  52.  
  53.  
  54.  
  55. // Function for server side chat
  56. void servfunc(int sockfd, char *ip, int cliport)
  57. {
  58. char buff[100];
  59. int n;
  60. bzero(buff, 100);
  61. read(sockfd, buff, sizeof(buff));
  62. printf("Message recieve from %s\n", ip);
  63. printf("Sender's Port: %d\n", cliport);
  64. printf("Message: %s\n", buff);
  65.  
  66. //chat back and forth with client
  67. /*
  68. char buff[100];
  69. int n;
  70. // infinite loop for chat
  71. for (;;) {
  72. bzero(buff, MAX);
  73.  
  74. // read the message from client and copy it in buffer
  75. read(sockfd, buff, sizeof(buff));
  76. // print buffer which contains the client contents
  77. printf("Type 'exit' to exit chat\n");
  78. printf("Message recieve from %s\n", ip);
  79. printf("Sender's Port: %d\n", cliport);
  80. printf("Message: %s\n", buff);
  81. printf("Message: ");
  82. bzero(buff, MAX);
  83. n = 0;
  84. // copy server message in the buffer
  85. while ((buff[n++] = getchar()) != '\n')
  86. ;
  87.  
  88. // and send that buffer to client
  89. write(sockfd, buff, sizeof(buff));
  90.  
  91. // if msg contains "Exit" then server exit and chat ended.
  92. if (strncmp("close", buff, 4) == 0) {
  93. printf("Server Exit...\n");
  94. break;
  95. }
  96. }
  97. */
  98. }
  99.  
  100.  
  101.  
  102.  
  103. //Function for client side chat
  104. void clifunc(int clifd, int portnum)
  105. {
  106. //chat back and forth with server
  107. /*
  108. char buff[100];
  109. int n;
  110. for (;;) {
  111. bzero(buff, sizeof(buff));
  112. printf("Type 'exit' to exit chat\n");
  113. printf("Message : ");
  114. n = 0;
  115. while ((buff[n++] = getchar()) != '\n')
  116. ;
  117. write(clifd, buff, sizeof(buff));
  118. printf("Message sent to port no: %d\n", portnum);
  119. bzero(buff, sizeof(buff));
  120. read(clifd, buff, sizeof(buff));
  121. printf("From Port %d: %s\n", portnum, buff);
  122. if ((strncmp(buff, "exit", 4)) == 0) {
  123. printf("Client Exit...\n");
  124. break;
  125. }
  126. }
  127. */
  128.  
  129. //write not working
  130.  
  131. char buff2[100];
  132. int o = 0;
  133. bzero(buff2, sizeof(buff2));
  134. printf("Enter the message: ");
  135. while((buff2[o++] = getchar()) != '\n')
  136. ;
  137. write(clifd, buff2, sizeof(buff2));
  138. bzero(buff2, sizeof(buff2));
  139. printf("Message sent to port: %d\n", portnum);
  140.  
  141. /*
  142. //one message
  143. //send cant take spaces
  144. char buff2[100];
  145. int o;
  146.  
  147. printf("Enter the message : ");
  148.  
  149. char msgtoserver[MAX];
  150. scanf("%s",msgtoserver);
  151. send(clifd, msgtoserver, strlen(msgtoserver), 0);
  152. bzero(buff2, sizeof(buff2));
  153. */
  154.  
  155. }
  156.  
  157. //for unblock
  158. int guard(int n, char* err){ if ( n == -1) { perror(err); exit(1); } return n;}
  159.  
  160. int main(int argc, char *argv[]){
  161. int sockfd, connfd, len;
  162. struct sockaddr_in servaddr, cli;
  163. int portnum;
  164. int choice;
  165. int done = 0;
  166.  
  167.  
  168. //for myip
  169. char hostbuffer[256];
  170. char *IPbuffer;
  171. struct hostent *host_entry;
  172. int hostname;
  173.  
  174. int connectyorn = 0;
  175.  
  176. int inputport;
  177. char *destination;
  178.  
  179. int ready = 0;
  180.  
  181. int portnumber;
  182.  
  183. //int myport;
  184. unsigned int myport;
  185.  
  186. //for send clientside
  187. struct sockaddr_in cliaddr, cliaddr2;
  188. int clifd;
  189.  
  190.  
  191.  
  192. if(argc < 2){
  193.  
  194. sockfd = guard(socket(AF_INET, SOCK_STREAM, 0), "could not create");
  195. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  196. int flags = guard(fcntl(sockfd, F_GETFL), "could not get flags");
  197. guard(fcntl(sockfd, F_SETFL, flags | O_NONBLOCK), "could not set to nonblocking");
  198. servaddr.sin_family = AF_INET;
  199. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  200. servaddr.sin_port = htons(0);
  201. guard(bind(sockfd, (SA*)&servaddr, sizeof(servaddr)), "could not bind");
  202. int length = sizeof(servaddr);
  203. getsockname(sockfd, (SA*)&servaddr, &length);
  204. myport = ntohs(servaddr.sin_port);
  205. printf("My port: %u \n", myport);
  206. guard(listen(sockfd, 100), "could not listen");
  207.  
  208.  
  209.  
  210. while(done != 9999){
  211. //added for unblock socket
  212. socklen_t addr_size;
  213. addr_size = sizeof(struct sockaddr_in);
  214. int clientfd = accept(sockfd, (SA*)&cli,&addr_size);
  215. if(clientfd == -1){
  216. if(errno == EWOULDBLOCK){
  217. printf("No pending connections.\n");
  218. sleep(1);
  219. }
  220. /*
  221. else{
  222. printf("error accepting connection");
  223. //exit(1);
  224. }
  225. */
  226. }
  227. else{
  228. //for single msg recieve
  229. /*
  230. char buff[MAX];
  231. bzero(buff, MAX);
  232.  
  233. // read the message from client and copy it in buffer
  234. read(clientfd, buff, sizeof(buff));
  235. //to get ip
  236. char ip[INET_ADDRSTRLEN];
  237. inet_ntop(AF_INET, &(cli.sin_addr),ip , INET_ADDRSTRLEN);
  238. //printf("Message recieve from: %s\n", inet_ntoa(servaddr.sin_addr));
  239. printf("Message recieve from: %s\n", ip);
  240. printf("Sender's Port: %d\n", ntohs(cli.sin_port));
  241. printf("Message: %s\n", buff);
  242. */
  243. char *ip = inet_ntoa(cli.sin_addr);
  244. int cliport = ntohs(cli.sin_port);
  245. servfunc(clientfd, ip, cliport);
  246. close(sockfd);
  247.  
  248. }
  249.  
  250.  
  251. printf("1.help\n");
  252. printf("2.myip\n");
  253. printf("3.myport\n");
  254. printf("4.connect <destination> <port no>\n");
  255. printf("5.list\n");
  256. printf("6.terminate <connection id.>\n");
  257. printf("7.send <connection id.> <message>\n");
  258. printf("8.exit\n");
  259. printf("9.Refresh\n");
  260.  
  261. scanf("%d", &choice);
  262.  
  263. switch(choice){
  264. case 1: printf("2. myip: Display IP address of the process.\n");
  265. printf("3. myport: Display the port on which the process is listening for incoming connections.\n");
  266. printf("4. connect: Connect to specified <destination> at the specified <port no>\n");
  267. printf("5. list: Display a numbered list of all the connections the process is part of.\n");
  268. printf("6. terminate: Terminate the connection listed under the specified number when LIST is used to display all connections.\n");
  269. printf("7. send: Send message to the host of the connect that is designated by the number 3 when command 'list' is used.\n");
  270. printf("8. exit: Close all connections and terminate the process.\n");
  271. break;
  272. case 2:
  273. // To retrieve hostname
  274. hostname = gethostname(hostbuffer, sizeof(hostbuffer));
  275. checkHostName(hostname);
  276.  
  277. // To retrieve host information
  278. host_entry = gethostbyname(hostbuffer);
  279. checkHostEntry(host_entry);
  280.  
  281. // To convert an Internet network
  282. // address into ASCII string
  283. IPbuffer = inet_ntoa(*((struct in_addr*)
  284. host_entry->h_addr_list[0]));
  285. checkIPbuffer(IPbuffer);
  286. //printf("Hostname: %s\n", hostbuffer);
  287. printf("Host IP: %s\n", IPbuffer);
  288. break;
  289. case 3:
  290. printf("My port: %u \n", myport);
  291. break;
  292. case 4:
  293.  
  294. printf("connect <destination> <port no>\n");
  295. // printf("Input destination: ");
  296. //char *destination;
  297. // scanf("%s", destination);
  298. printf("Input port no: ");
  299.  
  300. scanf("%d", &inputport);
  301.  
  302. portnum = inputport;
  303.  
  304.  
  305. // socket create and varification
  306. clifd = socket(AF_INET, SOCK_STREAM, 0);
  307. if (clifd == -1) {
  308. printf("socket creation failed...\n");
  309. exit(0);
  310. }
  311. else
  312. printf("Socket successfully created..\n");
  313.  
  314.  
  315. bzero(&servaddr, sizeof(servaddr));
  316.  
  317.  
  318. // assign IP, PORT
  319. cliaddr.sin_family = AF_INET;
  320. //127.0.0.1
  321. // servaddr.sin_addr.s_addr = inet_addr(destination);
  322. cliaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  323. cliaddr.sin_port = htons(portnum);
  324.  
  325. //added
  326. cliaddr2.sin_family = AF_INET;
  327. cliaddr2.sin_addr.s_addr = inet_addr("127.0.0.1");
  328. cliaddr2.sin_port = htons(myport);
  329. //cant bind to myport to get correct port number on clifunc
  330. if(bind(clifd, (SA*)&cliaddr2, sizeof(struct sockaddr_in)) == 0 ){
  331. printf("Binded\n");
  332. }
  333. else{
  334. printf("Unable to Bind\n");
  335. }
  336.  
  337. // connect the client socket to server socket
  338. if (connect(clifd, (SA*)&cliaddr, sizeof(cliaddr)) != 0) {
  339. printf("connection with the server failed...\n");
  340. //exit(0);
  341. }
  342. else{
  343. printf("connected to the server..\n");
  344. connectyorn = 1;
  345. ready = 1;
  346. }
  347. break;
  348. case 5: printf("list");
  349. break;
  350. case 6: printf("terminate");
  351. break;
  352. case 7:
  353. if(connectyorn == 1){
  354. // function for chat
  355. clifunc(clifd, portnum);
  356.  
  357. // close the socket
  358. close(clifd);
  359. }
  360. else
  361. printf("Not connected to server\n");
  362. break;
  363. case 8: printf("Closing program\n");
  364. done = 9999;
  365. break;
  366. case 9:
  367. break;
  368. }
  369.  
  370.  
  371. }
  372. }
  373. else{
  374.  
  375. portnum = atoi(argv[1]);
  376.  
  377. struct sockaddr_in cliaddr, cliaddr2;
  378. int clifd;
  379.  
  380.  
  381. // socket create and varification
  382. clifd = socket(AF_INET, SOCK_STREAM, 0);
  383. if (clifd == -1) {
  384. printf("socket creation failed...\n");
  385. exit(0);
  386. }
  387. else
  388. printf("Socket successfully created..\n");
  389. //bzero(&servaddr, sizeof(servaddr));
  390. //assign port and ip
  391. cliaddr.sin_family = AF_INET;
  392. cliaddr.sin_addr.s_addr = INADDR_ANY;
  393. cliaddr.sin_port = htons(portnum);
  394.  
  395. cliaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  396.  
  397. //explicity assign port by binding client with port
  398. cliaddr2.sin_family = AF_INET;
  399. cliaddr2.sin_addr.s_addr = INADDR_ANY;
  400. cliaddr2.sin_port = htons(0);
  401.  
  402. cliaddr2.sin_addr.s_addr = inet_addr("127.0.0.1");
  403.  
  404.  
  405. if(bind(clifd, (SA*)&cliaddr2, sizeof(struct sockaddr_in)) == 0 ){
  406. printf("Binded\n");
  407. }
  408. else{
  409. printf("Unable to Bind\n");
  410. }
  411.  
  412. int length = sizeof(cliaddr2);
  413. getsockname(clifd, (SA*)&cliaddr2, &length);
  414. myport = ntohs(cliaddr2.sin_port);
  415. printf("My port: %u \n", myport);
  416.  
  417.  
  418.  
  419. // connect the client socket to server socket
  420. if (connect(clifd, (SA*)&cliaddr, sizeof(cliaddr)) != 0) {
  421. printf("connection with the server failed...\n");
  422. exit(0);
  423. }
  424. else{
  425. printf("connected to the server..\n");
  426. // function for chat
  427. clifunc(clifd, portnum);
  428. // close the socket
  429. close(clifd);
  430. }
  431.  
  432. }
  433.  
  434.  
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement