Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <pthread.h>
- int valread;
- int PORTudp,PORTtcp;
- struct sockaddr_in address;
- typedef struct _thread_data_t {
- int tid;
- } thread_data_t;
- void *newInstanceTCP(void *vargp){
- thread_data_t *data = (thread_data_t *)vargp;
- int new_socket,server_fd;
- char buffer[1024] = {0};
- int opt = 1;
- int addrlen = sizeof(address);
- char *hello = "Hello from server\n";
- // Creating socket file descriptor
- if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
- perror("socket failed");
- exit(EXIT_FAILURE);
- }
- // Forcefully attaching socket to the port
- if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
- &opt, sizeof(opt)))
- {
- perror("setsockopt");
- exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons( PORTtcp );
- // Forcefully attaching socket to the port
- if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0){
- perror("bind failed");
- exit(EXIT_FAILURE);
- }
- if (listen(server_fd, 3) < 0){
- perror("listen");
- exit(EXIT_FAILURE);
- }
- if ((new_socket = accept(server_fd, (struct sockaddr *)&address,(socklen_t*)&addrlen))<0){
- perror("accept");
- exit(EXIT_FAILURE);
- }
- while(1){
- valread = read(new_socket , buffer, 1024);
- printf("%s\n",buffer );
- memset(buffer,0,strlen(buffer));
- send(new_socket , hello , strlen(hello) , 0 );
- printf("Hello message sent. Thread ID:%d \n",data->tid);
- }
- }
- void *newInstanceUDP(void *vargp){
- thread_data_t *data = (thread_data_t *)vargp;
- int new_socket,server_fd;
- char buffer[1024] = {0};
- int opt = 1;
- int addrlen = sizeof(address);
- char *hello = "Hello from server";
- struct sockaddr_in clientaddr;
- struct hostent *hostp;
- char *hostaddrp;
- // Creating socket file descriptor
- if ((server_fd = socket(AF_INET, SOCK_DGRAM, 0)) == 0){
- perror("socket failed");
- exit(EXIT_FAILURE);
- }
- // Forcefully attaching socket to the port
- if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
- &opt, sizeof(opt)))
- {
- perror("setsockopt");
- exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = htonl(INADDR_ANY);
- address.sin_port = htons((unsigned short)PORTudp );
- // Forcefully attaching socket to the port
- if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0){
- perror("bind failed");
- exit(EXIT_FAILURE);
- }
- int clientsize = sizeof(clientaddr);
- int buffersize=sizeof(buffer);
- int hellosize= sizeof(hello);
- while(1){
- if(recvfrom(server_fd, buffer, sizeof(buffer), 0, (struct sockaddr *) &clientaddr, &clientsize)<0){
- error("receive failed");
- exit(EXIT_FAILURE);
- }
- printf("From Client: %s\n",buffer );
- memset(buffer,0,strlen(buffer));
- sendto(server_fd , hello , hellosize , 0, &clientaddr, clientsize );
- printf("Hello message sent. Thread ID:%d \n",data->tid);
- }
- }
- int main(int argc, char const *argv[])
- {
- PORTtcp = atoi(argv[1]);
- PORTudp = atoi(argv[2]);
- pthread_t tid[200];
- thread_data_t thr_data[200];
- for(int i=0;i<100;i++){
- thr_data[i].tid=i;
- pthread_create(&tid[i], NULL, newInstanceTCP, &thr_data[i]);
- }
- for(int i=100;i<200;i++){
- thr_data[i].tid=i;
- pthread_create(&tid[i], NULL, newInstanceUDP, &thr_data[i]);
- }
- while(1){
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement