day1:
long get_long()
{
long input;
int ch;
while(scanf("%ld", &input) != 1)
{
printf("%ld", input);
while((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an integer . Please enter an integer value \n");
}
return input;
}
day2:
int m_gets(void)
{
//给指针赋值一定要记得分配空间
char sBuffer[STLEN];
char *p;
p = sBuffer;
scanf("%s", p);
char *name;
// malloc()与free()配合使用
name = (char *)malloc(STLEN);
scanf("%s", name);
puts(name);
getchar();
//gets只有一个参数,可能导致缓冲区溢出
char words[STLEN];
puts("Enter a string :");
//gets(words);
fgets(words, STLEN, stdin);
printf("%s", words);
puts(words);
puts("Done.");
return 0;
}
day3:
#include <stdio.h>
#include <stdlib.h>
static int sum(int value);
struct inout{
int value;
int result;
};
int main (int argc, char *argv[]) {
struct inout *io = (struct inout*) malloc (sizeof(struct inout));
if (io == NULL)
{
printf("申请内存失败\n");
return -1;
}
if (argc !=2)
{
printf("参数输入错误!\n");
return -1;
}
io->value = *argv[1] - '0';
io->result = sum(io->value);
printf("您输入的值为: %d, 结果为: %d\n", io->value, io->result);
}
static int sum(int value) {
int result = 0;
int i = 0;
for(i=0;i<value;i++)
result +=i;
return result;
}
GDB
gdb ·调试前一天的程序
gcc -g xxx.c
gdb a.out
b 15 断点
run 4 传参 运行
print argc
print argv[1]
b 36
c 下一步
display i
display result 每一步都显示这倆值
n
n
n
finish 结束函数
bt 显示函数调用树
info thread 查看进程中的线程号
thread id 进入线程
...
clear 清除断点
whatis 查看变量类型
ptype 结构的详细定义
tcp srv
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <pthread.h>
#define PORT_NUMBER 8501
void *closefd(void *fd) {
close((int)fd);
}
void *pthexec(void *accept_fd)
{
int nbytes;
char buffer[1024];
pthread_cleanup_push((void *)closefd,accept_fd);
if ((nbytes = read((int)accept_fd, buffer, 1024)) == -1) {
printf("read error %s \n\a",strerror(errno));
exit(1);
}
buffer[nbytes] = '\0';
printf("server received %s \n", buffer);
pthread_cleanup_pop(1);
}
int main() {
int n;
int accept_fd,listen_fd;
struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size; int nbytes; char buffer[1024];
if ((listen_fd= socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) == -1)
{
fprintf(stderr, "Socket err : %s \n \a", strerror(errno));
exit(1);
}
bzero(&server_addr, sizeof(struct sockaddr_in));
//ipv4
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT_NUMBER);
n = 1;
setsockopt(listen_fd,SOL_SOCKET,SO_REUSEADDR,&n, sizeof(int));
if (bind(listen_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1 )
{
fprintf(stderr, "Bind error : %s \n",strerror(errno));
exit(1);
}
if (listen(listen_fd, 5) == -1)
{
fprintf(stderr, "Listen error : %s \n",strerror(errno));
exit(1);
}
pthread_t pth;
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if ((accept_fd = accept(listen_fd, (struct sockaddr *)(&server_addr), &sin_size)) == -1)
{
fprintf(stderr, "Accept error %s \n\a",strerror(errno));
exit(1);
}
if ((accept_fd < 0 ) && (errno == EINTR)) {
continue;
} else if (accept_fd < 0 ) {
printf("accept error %s \n\a",strerror(errno));
continue;
}
n = pthread_create(&pth,NULL,pthexec,(void *)accept_fd);
if (n == 0) {
printf("execute succ");
} else {
printf("execute err");
close(accept_fd);
}
}
}
UDP SRV
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT_NUMBER 8511
void udps_respon(int sockfd) {
int nbytes;
int MAX_SIZE = 1024;
char msg[MAX_SIZE];
struct sockaddr_in from_addr;
int sin_size = sizeof(struct sockaddr_in);
while (1) {
nbytes = recvfrom(sockfd, msg, MAX_SIZE, 0, (struct sockaddr *) &from_addr, (socklen_t *) &sin_size);
if (nbytes == -1) {
exit(1);
}
msg[nbytes] = 0;
//add send
int s = sendto(sockfd,msg,strlen(msg),0,&from_addr,sin_size);
fprintf(stdout, " - server have received %s \n", msg);
}
}
int main()
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
char buffer[1024];
if ((sockfd= socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP)) == -1)
{
fprintf(stderr, "Socket err : %s \n \a", strerror(errno));
exit(1);
}
bzero(&server_addr, sizeof(struct sockaddr_in));
//ipv4
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORT_NUMBER);
if (bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1 )
{
fprintf(stderr, "Bind error : %s \n",strerror(errno));
exit(1);
}
udps_respon(sockfd);
close(sockfd);
}