Skip to content
Snippets Groups Projects
Commit d9c9f2a1 authored by XU CHENGLIN's avatar XU CHENGLIN
Browse files

Initial commit

parents
Branches
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#define MAX_LEN 1024
typedef struct {
int sockFD;
int port;
} acceptor;
int initAcceptor(int port, acceptor *instance)
{
int result = -1;
struct sockaddr_in sockaddr;
if (instance != NULL) {
instance->port = port;
instance->sockFD = socket(AF_INET, SOCK_DGRAM, 0);
if (instance->sockFD <= 0) {
perror("Fail creating socket.");
return result;
};
} else {
perror("Fail allocating memory for Acceptor instance.");
return result;
}
bzero(&sockaddr, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
sockaddr.sin_port = htons(port);
result = bind(instance->sockFD, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (result < 0) {
printf("skFD: %d, bd rst: %d\n", instance->sockFD, result);
perror("Acceptor fail binding.");
};
return result;
}
int main(int argc, char **argv)
{
/**
* TODO:
* Check @param argc and @param argv for exceptions
* Build a log system instead of using @code `printf()`
*
*/
if (argc < 2) {
printf("usage: %s <Port>\n", argv[0]);
return 1;
}
char buffer[MAX_LEN];
int msgLen = 0;
char *next;
int port = strtol(argv[1], &next, 10);
acceptor acpt;
initAcceptor(port, &acpt);
if (acpt.sockFD <= 0) {
printf("Port %d\n", port);
perror("Failed creating acceptor.");
free(buffer);
return 1;
}
/**
* This call returns the number of bytes received, or -1 if an error occurred.
* For TCP sockets, the return value 0 means the peer has closed its half side of the connection.
*/
msgLen = recv(acpt.sockFD, buffer, MAX_LEN, 0);
return 0;
}
\ No newline at end of file
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
const char filename[] = "hello";
void* thread(void *id){
int num = *(int *)id;
// 加锁
if (pthread_mutex_lock(&mutex) != 0){
fprintf(stdout, "lock error!\n");
}
printf("Thread <%d> inner mutex locked.\n", num);
// 写文件的操作
FILE *fp = fopen(filename, "a+");
int start = *((int *)id);
int end = start + 1;
setbuf(fp, NULL);// 设置缓冲区的大小
fprintf(stdout, "%d\n", start);
for (int i = (start * 10); i < (end * 10); i ++){
fprintf(fp, "%d\t", i);
}
fprintf(fp, "\n");
fclose(fp);
pthread_cond_signal(&cond);
// 解锁
pthread_mutex_unlock(&mutex);
printf("Thread <%d> inner mutex unlocked.\n", num);
return NULL;
}
int main(){
int num_thread = 5;
pthread_t *pt = (pthread_t *)malloc(sizeof(pthread_t) * num_thread);
int * id = (int *)malloc(sizeof(int) * num_thread);
// 初始化互斥锁
if (pthread_mutex_init(&mutex, NULL) != 0 || pthread_cond_init(&cond, NULL) != 0){
// 互斥锁初始化失败
free(pt);
free(id);
return 1;
}
for (int i = 0; i < num_thread; i++){
id[i] = i;
// 加锁
if (pthread_mutex_lock(&mutex) != 0){
fprintf(stdout, "lock error!\n");
}
printf("Thread main mutex locked.\n");
if (pthread_create(&pt[i], NULL, thread, &id[i]) != 0){
printf("thread create failed!\n");
return 1;
}
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
printf("Thread main mutex unlocked.\n");
}
for (int i = 0; i < num_thread; i++){
pthread_join(pt[i], NULL);
}
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
// 释放资源
free(pt);
free(id);
return 0;
}
\ No newline at end of file
sender.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#define MAX_LEN 1024
typedef struct {
int sockFD;
struct sockaddr dest;
} sender;
int initAcceptor(const char *destIP, int port, sender *instance)
{
int result = -1;
struct sockaddr_in *sockaddr = (struct sockaddr_in*) &instance->dest;
if (instance != NULL) {
instance->sockFD = socket(AF_INET, SOCK_DGRAM, 0);
if (instance->sockFD <= 0) {
perror("Fail creating socket.");
return result;
};
} else {
perror("Fail allocating memory for Acceptor instance.");
return result;
}
bzero(&sockaddr, sizeof(sockaddr));
sockaddr->sin_family = AF_INET;
sockaddr->sin_addr.s_addr = inet_addr(destIP);
sockaddr->sin_port = htons(port);
result = 0;
return result;
}
int main(int argc, char **argv)
{
/**
* TODO:
* Check @param argc and @param argv for exceptions
* Build a log system instead of using @code `printf()`
*
*/
if (argc < 2) {
printf("usage: %s <Port>\n", argv[0]);
return 1;
}
char buffer[MAX_LEN];
int msgLen = 0;
char *next;
int port = strtol(argv[2], &next, 10);
sender client;
initAcceptor(argv[1] ,port, &client);
if (client.sockFD <= 0) {
printf("Port %d\n", port);
perror("Failed creating acceptor.");
free(buffer);
return 1;
}
/**
* This call returns the number of bytes received, or -1 if an error occurred.
* For TCP sockets, the return value 0 means the peer has closed its half side of the connection.
*/
msgLen = sendto(client.sockFD, buffer, strlen(buffer), 0, &(client.dest), sizeof(client.dest));
return 0;
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment