📄 tcp_concurrent thread.c.txt
字号:
///* ---------------Homework2 Problem-1---------------------------------- */
/* Submitted by :Santa Kunchala (SJSU ID: 005859672)--------------------*/
/* Program Title :thread_concurrent.c (TCP)- -------------------------------------*/
/* Problem-1 :Write the multithread concurrent server program that receives request from
client and send requesed byte to client. */
#include <sys/time.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h> /* socket*/
#include <sys/socket.h> /*socket*/
#include <arpa/inet.h> /*inet_pton*/
#include <stdlib.h> /*for exit */
#include <netdb.h> /*for strcut sockaddr_in size*/
#include <strings.h> /*for bzero */
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <pthread.h>
#define MAXLINE 100
#define SENDBUF 10000
#define SERV_PORT 8001
#define LISTENQ 1000
int main(int argc, char **argv)
{
int sockfd, connfd, nthread;
pthread_t tid;
socklen_t clilen, addrlen;
struct sockaddr *cliaddr;
struct sockaddr_in servaddr;
void* doit(void *);
void sig_int(int);
/* Creating a Socket;*/
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("socket");
exit(0);
}
/* Initializing the sockaddr_in servaddr structure to zero*/
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){
perror("bind");
exit(0);
}
if (listen(sockfd, LISTENQ) < 0){
perror("listen");
exit(0);
}
addrlen = sizeof(struct sockaddr);
if( (cliaddr = malloc(addrlen)) == NULL){
printf("could allocate memory to store child address \n");
}
signal(SIGINT, sig_int);
for(;;){
clilen = addrlen;
if((connfd = accept (sockfd, cliaddr, &clilen)) < 0) {
if (errno == EINTR){
continue;
}else{
perror("accept error");
exit(0);
}
}
if ((nthread = pthread_create(&tid, NULL, &doit, (void *) connfd)) < 0){
errno = nthread;
perror("Thread create");
exit(0);
}
}
}
void* doit(void *arg)
{
int towrite, end_data;
thread_t *func_arg = (thread_t *)arg;
int ntowrite = func_arg->ntowrite;
int sockfd = func_arg->sockfd;
struct sockaddr_in cliaddr= func_arg->address;
int n, n1, n2, send_data;
char buf[SENDBUF];
memset(buf, 1, sizeof(buf));
if ((n = pthread_detach(pthread_self())) < 0){
n = errno;
perror("pthread detach error \n");
return(NULL);
}
if(ntowrite <= 0){
printf(" Bytes to be written Invalid \n");
}
while( ntowrite > 0){
towrite = ( ntowrite < sizeof(buf)) ? ntowrite : sizeof(buf);
printf("Value being written into the buffer %d\n ", towrite);
if( (n1 = pthread_mutex_lock(&mlock))< 0){
errno = n1;
perror("Thread lock error");
return(NULL);
}
if ((send_data = sendto(sockfd, buf, towrite, 0, (struct sockaddr *) &cliaddr,
sizeof(cliaddr))) < 0){
perror("write error");
close(sockfd);
exit(0);
}
if ((n2 = pthread_mutex_unlock(&mlock)) < 0){
errno = n2;
perror("Thread unlock error");
return(NULL);
}
ntowrite -= towrite;
printf("Value of ntowrite is : %d \n", ntowrite);
}
if (end_data >= 1000000){
char buf_1[SENDBUF];
memset(buf_1, 0x0, sizeof(buf_1));
sleep(1);
if((send_data = sendto(sockfd, buf_1, sizeof(buf_1), 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr))) < 0){
perror("write error in END BYTE");
close(sockfd);
exit(0);
}
}
fflush(NULL);
free(func_arg);
return(NULL);
}
void sig_int(int signo)
{
exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -