client:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
int main(int argc,char *argv[]){
int cli_fd,ser_fd;
struct sockaddr_in cli_addr,ser_addr;
memset(&ser_addr,0,sizeof(ser_addr));
memset(&cli_addr,0,sizeof(cli_addr));
cli_fd = socket(AF_INET,SOCK_STREAM,0);
ser_addr.sin_family=AF_INET;
ser_addr.sin_port=htons(10934);
inet_pton(AF_INET,"192.168.1.176",&ser_addr.sin_addr.s_addr);
if(bind(cli_fd,(struct sockaddr*)&cli_addr,sizeof(cli_addr))==-1){
perror("bind");
exit(-1);
}
if(connect(cli_fd,(struct sockaddr*)&ser_addr,sizeof(ser_addr))==-1){
perror("connect");
exit(-1);
}
printf("connecting\n");
}
=====================================================================================
=====================================================================================
server:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
int main(int argc,char *argv[]){
int sock_fd,new_fd;
struct sockaddr_in ser_addr;
struct sockaddr_in cli_addr;
socklen_t len =sizeof(cli_addr);
sock_fd = socket(AF_INET,SOCK_STREAM,0);
if(sock_fd==-1){
perror("socket");
exit(-1);
}
memset(&ser_addr,0,sizeof(ser_addr));
memset(&cli_addr,0,sizeof(cli_addr));
ser_addr.sin_family=AF_INET;
ser_addr.sin_port=htons(10934);
ser_addr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sock_fd,(struct sockaddr*)&ser_addr,sizeof(ser_addr))<0){
perror("bind");
}
if(listen(sock_fd,5)==-1){
perror("listen");
exit(-1);
}
while(1){
new_fd = accept(sock_fd,(struct sockaddr*)&cli_addr,&len);
if(new_fd==-1){
perror("accept");
exit(-1);
}
int ip1=cli_addr.sin_addr.s_addr;//ntohl((cli_addr.sin_addr).s_addr);
char *str;
str=(char *)malloc(sizeof(char)*4);
if(inet_ntop(AF_INET,&ip1,str,INET_ADDRSTRLEN)==NULL){
perror("inet_ntop");
exit(-1);
}
printf("%s\n",str);
printf("connecting\n");
break;
}
}