① 在linux下如何用c语言来写一个socket编程的聊天小程序
你要去学习linux下进行网络编程的一些函数,比如socket(),listen,accept()等函数.再看点例子就会了
② C语言菜鸟自写一个小程序在linux上编译后提示Segmentation fault (core mped),求帮助
i != '\0' 改成 *i != '\0', 另一处j !='\0'也一样
③ 在Linux下用C语言编程
4。守护进程的创建
如果你在DOS时代编写过程序,那么你也许知道在DOS下为了编写一个常驻内存的程序我们要编写多少代码了.相反如果在Linux下编写一个"常驻内存"的程序却是很容易的.我们只要几行代码就可以做到. 实际上由于Linux是多任务操作系统,我们就是不编写代码也可以把一个程序放到后台去执行的.我们只要在命令后面加上&符号SHELL就会把我们的程序放到后台去运行的. 这里我们"开发"一个后台检查邮件的程序.这个程序每个一个指定的时间回去检查我们的邮箱,如果发现我们有邮件了,会不断的报警(通过机箱上的小喇叭来发出声音). 后面有这个函数的加强版本加强版本
后台进程的创建思想: 首先父进程创建一个子进程.然后子进程杀死父进程(是不是很无情?). 信号处理所有的工作由子进程来处理.
#include
#include
#include
#include
#include
#include
#include
/* Linux 的默任个人的邮箱地址是 /var/spool/mail/用户的登录名 */
#define MAIL "/var/spool/mail/hoyt"
/* 睡眠10秒钟 */
#define SLEEP_TIME 10
main(void)
{
pid_t child;
if((child=fork())==-1)
{
printf("Fork Error:%s\n",strerror(errno));
exit(1);
}
else if(child>0)
while(1);
if(kill(getppid(),SIGTERM)==-1)
{
printf("Kill Parent Error:%s\n",strerror(errno));
exit(1);
}
{
int mailfd;
while(1)
{
if((mailfd=open(MAIL,O_RDONLY))!=-1)
{
fprintf(stderr,"%s","\007");
close(mailfd);
}
sleep(SLEEP_TIME);
}
}
}
你可以在默认的路径下创建你的邮箱文件,然后测试一下这个程序.当然这个程序还有很多地方要改善的.我们后面会对这个小程序改善的,再看我的改善之前你可以尝试自己改善一下.比如让用户指定邮相的路径和睡眠时间等等.相信自己可以做到的.动手吧,勇敢的探险者.
好了进程一节的内容我们就先学到这里了.进程是一个非常重要的概念,许多的程序都会用子进程.创建一个子进程是每一个程序员的基本要求!
④ 高手帮忙写一个linux下的c语言小程序
#include<stdio.h>
int main(int argc, char ** argv)
{
int i;
char a[20];
gets(a);
if (argc==1)
{
for(i=0;i<strlen(a);i++)
{
if(a[i] > 64 && a[i] < 91)
a[i]=a[i]+32;
putchar(a[i]);
}
putchar('\n');
}
for(i=1;i<argc;i++)
{
if(!strcmp(argv[i],"-c"))
{
puts(a);
exit(0);
}
if(!strcmp(argv[i],"-x"))
{
printf("error\n");
exit(1);
}
}
}
//在GCC中编译好了就行了
⑤ 如何在linux下用c语言编写一个能够发送icmp报文的小程序
需要建立socket,参数是AF_INET,SOCK_RAW,IPPROTO_ICMP
自己构造ICMP数据包,sendto发送给某地址。
ICMP有多种,你可以发送type为13的时间戳请求。
然后调用recvfrom会收到type为14的timestampreply的IP包,
IP头一般是20Bytes,里面包含srcIP,desIP还有TTL等。
IP包的数据就是返回ICMPtimestampreply报文,里面有origTimestamp,recvTimestamp,transStamp,可以计算出时间。
可以参考网页链接网页链接
觉得有帮助可以注册帐号,给他点个“星”
⑥ linux下c语言 远程控制小程序
亲……我也在研究信号和线程,你出现这个问题是因为内存是有限的,只允许一个程序运行,那么另一个程序自然就堵塞在一边了,如果被堵的不是主程序,主程序结束了自然就全结束了。建议你去看看互斥锁 ,如果还不明白可以接着问……
⑦ 在linux下如何用c语言来写一个socket编程的聊天小程序
源码如下:
//chat_one.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <sys/select.h>
int main( int argc, char **argv)
{
struct sockaddr_in chatone, chattwo;
char pmsg[1000];
char *buf;
int chatone_fd;
int ret,i;
int len, msg_len;
fd_set fdset;
if ( argc < 2 ){
printf("please input ip address\n");
return -1;
}
printf("server address is %s\n", argv[1]);
chattwo.sin_family = AF_INET;
chattwo.sin_port = htons(60002);
inet_pton(AF_INET, argv[1], &chattwo.sin_addr.s_addr);
chatone.sin_family = AF_INET;
chatone.sin_port = htons(60000);
chatone.sin_addr.s_addr = INADDR_ANY;
chatone_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( -1 == chatone_fd ){
printf("create socket failed %s\n", strerror(errno));
return -1;
}
ret = bind(chatone_fd, (struct sockaddr *)&chatone, sizeof(chatone));
if ( -1 == ret){
printf("bind failed %s \n", strerror(errno));
return -1;
}
for(i=0; i<1000;i++){
FD_ZERO( &fdset );
FD_SET ( 0, &fdset);
FD_SET( chatone_fd, &fdset);
if ( -1 == select ( chatone_fd+1, &fdset, NULL, NULL, NULL) ){
continue;
}
if ( FD_ISSET( chatone_fd, &fdset)){
recvfrom( chatone_fd, pmsg, 999, 0, NULL, 0);
printf("receive %s\n", pmsg);
}
else{
memset( pmsg, 0, 1000);
fgets(pmsg, 999, stdin);
len = sizeof(chattwo);
sendto( chatone_fd, pmsg, 1000, 0,\
(struct sockaddr*) &chattwo, len);
printf("send %s\n", pmsg);
}
}
printf("sent %d packets\n", i);
close(chatone_fd);
return 0;
}
//chat_two.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <sys/select.h>
int main( int argc, char **argv)
{
struct sockaddr_in chatone, chattwo;
char pmsg[1000];
char *buf;
int chattwo_fd;
int ret,i;
int len, msg_len;
fd_set fdset;
if ( argc < 2 ){
printf("please input ip address\n");
return -1;
}
printf("server address is %s\n", argv[1]);
chattwo.sin_family = AF_INET;
chattwo.sin_port = htons(60002);
chattwo.sin_addr.s_addr = INADDR_ANY;
chatone.sin_family = AF_INET;
chatone.sin_port = htons(60000);
inet_pton(AF_INET, argv[1], &chatone.sin_addr.s_addr);
chattwo_fd = socket(PF_INET, SOCK_DGRAM, 0);
if ( -1 == chattwo_fd ){
printf("create socket failed %s\n", strerror(errno));
return -1;
}
ret = bind(chattwo_fd, (struct sockaddr *)&chattwo, sizeof(chattwo));
if ( -1 == ret){
printf("bind failed %s \n", strerror(errno));
return -1;
}
for(i=0; i<1000;i++){
FD_ZERO( &fdset );
FD_SET ( 0, &fdset);
FD_SET( chattwo_fd, &fdset);
if ( -1 == select ( chattwo_fd+1, &fdset, NULL, NULL, NULL) ){
continue;
}
if ( FD_ISSET( chattwo_fd, &fdset)){
recvfrom( chattwo_fd, pmsg, 999, 0, NULL, 0);
printf("receive: %s\n", pmsg);
}
else{
memset( pmsg, 0, 1000);
fgets(pmsg, 999, stdin);
len = sizeof(chatone);
sendto( chattwo_fd, pmsg, 1000, 0,\
(struct sockaddr*) &chatone, len);
printf("send %s\n", pmsg);
}
}
printf("sent %d packets\n", i);
close(chattwo_fd);
return 0;
}
编译好这个两个程序就可以进行简单的通信了。
⑧ 用linux下的c语言读取txt文件中的列数据
1.用fgets函数可以读取文件中某行的数据,某列数据就必须一个一个读入每行的第几个字符,再存入到一个字符串当中。
2.例程:
#include<stdio.h>
#include<string.h>
voidmain()
{
chara[100],b[100],c[100];
inti=3,j=4,k=0;//第三行,第四列
FILE*fp=fopen("data.txt","r");
while(fgets(c,100,fp)){//读入每行数据
i--;
if(i==0)strcpy(a,c);//读到第三行数据
b[k++]=c[j-1];//把每行的那列字符拷到b中
}
b[k]=0;
printf("第%d行数据:%s ",i,a);
printf("第%d列数据:%s ",j,b);
fclose(fp);
}
⑨ linux系统下,c语言pthread多线程编程传参问题
3个线程使用的都是同一个info
代码 Info_t *info= (Info_t *)malloc(sizeof(Info_t));只创建了一个info
pthread_create(&threads[i],NULL,calMatrix,(void *)info); 三个线程使用的是同一个
我把你的代码改了下:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
intmtc[3]={0};//resultmatrix
typedefstruct
{
intprank;
int*mta;
int*mtb;
}Info_t;
void*calMatrix(void*arg)
{
inti;
Info_t*info=(Info_t*)arg;
intprank=info->prank;
fprintf(stdout,"calMatrix:prankis%d ",prank);
for(i=0;i<3;i++)
mtc[prank]+=info->mta[i]*info->mtb[i];
returnNULL;
}
intmain(intargc,char**argv)
{
inti,j,k=0;
intmta[3][3];
intmtb[3]={1};
Info_t*info=(Info_t*)malloc(sizeof(Info_t)*3);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
mta[i][j]=k++;
/*3threads*/
pthread_t*threads=(pthread_t*)malloc(sizeof(pthread_t)*3);
fprintf(stdout," ");fflush(stdout);
for(i=0;i<3;i++)
{
info[i].prank=i;
info[i].mta=mta[i];
info[i].mtb=mtb;
pthread_create(&threads[i],NULL,calMatrix,(void*)(&info[i]));
}
for(i=0;i<3;i++)
pthread_join(threads[i],NULL);
fprintf(stdout," ====thematrixresult==== ");
fflush(stdout);
for(i=0;i<3;i++)
{
fprintf(stdout,"mtc[%d]=%d ",i,mtc[i]);
fflush(stdout);
}
return0;
}
矩阵的计算我忘记了,你运行看看结果对不对
⑩ 怎么编写C语言程序,如:Helloworld的
代码如下:
#include<stdio.h>
intmain(void)
{
printf("Hello,world!");
return0;
}
一、首先,打开我们的Visual C++ 6.0软件,我使用的为中文版,软件主界面如下图所示:
(10)linux下c语言小程序扩展阅读:
C语言的基本构成:
1、数据类型。C的数据类型包括:整型、字符型、实型或浮点型(单精度和双精度)、枚举类型、数组类型、结构体类型、共用体类型、指针类型和空类型。
2、常量与变量。常量其值不可改变,符号常量名通常用大写。变量是以某标识符为名字,其值可以改变的量。标识符是以字母或下划线开头的一串由字母、数字或下划线构成的序列,请注意第一个字符必须为字母或下划线,否则为不合法的变量名。变量在编译时为其分配相应存储单元。
3、数组。如果一个变量名后面跟着一个有数字的中括号,这个声明就是数组声明。字符串也是一种数组。它们以ASCII的NULL作为数组的结束。要特别注意的是,方括内的索引值是从0算起的。
4、指针。
(1)、如果一个变量声明时在前面使用 * 号,表明这是个指针型变量。换句话说,该变量存储一个地址,而 *(此处特指单目运算符 * ,下同。C语言中另有 双目运算符 *) 则是取内容操作符,意思是取这个内存地址里存储的内容。指针是 C 语言区别于其他同时代高级语言的主要特征之一
(2)、指针不仅可以是变量的地址,还可以是数组、数组元素、函数的地址。通过指针作为形式参数可以在函数的调用过程得到一个以上的返回值,不同于return(z)这样的仅能得到一个返回值。
(3)指针是一把双刃剑,许多操作可以通过指针自然的表达,但是不正确的或者过分的使用指针又会给程序带来大量潜在的错误。
5、字符串。C语言的字符串其实就是以'