Skip to main content

Posts

Showing posts from January, 2014

tcp client, server chat simplified

Here is my simplified version of the tcp client, server chat application client.c #include "stdio.h" #include "stdlib.h" #include "sys/types.h" #include "sys/socket.h" #include "arpa/inet.h" int main() { int sockfd,n; char buff[256]; struct sockaddr_in servaddr; //create socket sockfd = socket(AF_INET,SOCK_STREAM,0); //fill serv struct servaddr.sin_family = AF_INET; servaddr.sin_port = htons(40402); servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

Executing external programs via c

import time print time.ctime() this snippet will print the system time. in my networking lab, i used this python snippet to print the system time. actually i have to write a client, server program in c to achieve this. but i have found a lazy way. when mam ask us to show the output i planned to run this python snippet :) (don't tell her); only problem is i cannot make it as an executable code. ./timecli

simple tcp echo server

echoserver.c //coded by m4n1G //6-1-2014 //tcp echo server in unix env #include "stdlib.h" #include "sys/types.h" #include "sys/socket.h" #include "netinet/in.h" #include "arpa/inet.h" int main(int argc, char** argv) { int server_fd, client_fd; char c; struct sockaddr_in serv_addr; //create a socket server_fd = socket(AF_INET,SOCK_STREAM,0); //getting ready to bind serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(atoi(argv[1])); serv_addr.sin_addr.s_addr = INADDR_ANY; //bind bind(server_fd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)); //listen to one queue listen(server_fd,1); //accept one client client_fd = accept(server_fd, NULL, NULL); //read and write until ^D from client while(read(client_fd, &c, 1)) { //read from client putchar((int)c); write(client_fd, &c, 1); // write to client } //close the connection