//client.c///
#include<stdio.h>
#include<unistd.h>
#include<strings.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#define PORT 1234
#define MAXDATASIZE 100
void *func(void *arg);
int main ( int argc, char *argv[])
{
pthread_t tid;
int sockfd, num;
char buf[MAXDATASIZE] ="123456789";
struct hostent *he;
struct sockaddr_in server;
if(argc!=2)
{
printf("usage %s<ip address>/n",argv[0]);
exit(1);
}
if((he = gethostbyname(argv[1])) == NULL)
{
printf("gethostbyname error/n");
exit(1);
}
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("socket() error /n");
exit(1);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr = *((struct in_addr *)he->h_addr);
if(connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1)
{
printf("connetc() error/n");
exit(1);
}
if(pthread_create(&tid, NULL, func, (void *)&sockfd))
{
perror("error pthread_create");
return 2;
}
while(1)
{
num= recv(sockfd, buf, MAXDATASIZE, 0);
printf("server message: %s/n",buf);
}
close(sockfd);
}
void *func(void *arg)
{
int *sockfd;
char buf[MAXDATASIZE];
sockfd = (int *)arg;
while(1)
{
fgets(buf, MAXDATASIZE, stdin);
send(*sockfd, buf, MAXDATASIZE, 0);
}
}
...............................................................
/******************server.c*************/
#include<stdio.h>
#include<strings.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define PORT 1234
#define BACKLOG 1
#define MAXDATASIZE 100
struct ARG
{
int connfd;
struct sockaddr_in client;
};
void *func(void *arg );
int main()
{
pthread_t tid;
int listenfd, connectfd;
struct sockaddr_in server;
struct sockaddr_in client;
socklen_t addrlen;
char buf[MAXDATASIZE];
struct ARG *arg, in;
arg = ∈
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket() error.");
exit(1);
}
int opt = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1)
{
perror("bind() error.");
exit(1);
}
if(listen(listenfd, BACKLOG) == -1)
{
perror("listen() error.");
exit(1);
}
addrlen = sizeof(client);
while(1)
{
if((connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen )) == -1)
{
perror("accept() error.");
exit(1);
}
printf("You got a connection from client ip is: %s,port is:%d/n",inet_ntoa(client.sin_addr), htons(client.sin_port));
//arg = (struct ARG *)malloc(sizeof(struct ARG));
arg->connfd = connectfd;
arg->client = client;
if(pthread_create(&tid, NULL, func, (void *)arg))
{
perror("error pthread_create");
exit(1);
}
while(1)
{
recv(connectfd, buf, MAXDATASIZE,0);
printf("the client says: %s/n",buf);
}
close(connectfd);
close(listenfd);
}
}
void *func(void *arg)
{
char buf[MAXDATASIZE];
struct ARG *info;
info = (struct ARG *)arg;
while(1)
{
fgets(buf, MAXDATASIZE, stdin);
send(info->connfd, buf, MAXDATASIZE, 0);
}
}