Last active
August 29, 2015 14:05
-
-
Save fjorgemota/e29072321e7f74e2eb12 to your computer and use it in GitHub Desktop.
Mini-Servidor em C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <iostream> | |
#include <sys/socket.h> | |
#include <sys/types.h> | |
#include <netinet/in.h> | |
using namespace std; | |
int main(int argc, char **argv) { | |
int sock, client; | |
cout << "Hello World\n" << flush; | |
sock= socket(AF_INET, SOCK_STREAM, 0); | |
if(sock < 0) { | |
cerr << "Socket nao pode ser criado\n" << flush; | |
return EXIT_FAILURE; | |
} | |
struct sockaddr_in addr, client_addr; | |
addr.sin_family = AF_INET; | |
addr.sin_port = htons(atoi("8585")); | |
addr.sin_addr.s_addr = INADDR_ANY; | |
bind(sock, (sockaddr *) &addr, sizeof(addr)); | |
listen(sock, 1000); | |
cout << "Escutando na porta 8585\n" << flush; | |
char response[] = "HTTP/1.1 200\r\nContent-Type: text/plain\r\n\r\nHello World"; | |
while(true) { | |
socklen_t clilen = sizeof(client_addr); | |
client = accept(sock, (struct sockaddr *) &client_addr, &clilen); | |
cout << "Recebida conexao\n" << flush; | |
if(client < 0) { | |
cerr << "Socket nao pode ser aceito\n" << flush; | |
} | |
write(client, &response, sizeof(response)-1); | |
close(client); | |
} | |
close(sock); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.file "hello.cpp" | |
.local _ZStL8__ioinit | |
.comm _ZStL8__ioinit,1,1 | |
.section .rodata | |
.LC0: | |
.string "Hello World\n" | |
.LC1: | |
.string "Socket nao pode ser criado\n" | |
.LC2: | |
.string "8585" | |
.LC3: | |
.string "Escutando na porta 8585\n" | |
.LC4: | |
.string "Recebida conexao\n" | |
.LC5: | |
.string "Socket nao pode ser aceito\n" | |
.text | |
.globl main | |
.type main, @function | |
main: | |
.LFB970: | |
.cfi_startproc | |
pushq %rbp | |
.cfi_def_cfa_offset 16 | |
.cfi_offset 6, -16 | |
movq %rsp, %rbp | |
.cfi_def_cfa_register 6 | |
subq $144, %rsp | |
movl %edi, -132(%rbp) | |
movq %rsi, -144(%rbp) | |
movq %fs:40, %rax | |
movq %rax, -8(%rbp) | |
xorl %eax, %eax | |
movl $.LC0, %esi | |
movl $_ZSt4cout, %edi | |
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc | |
movl $_ZSt5flushIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi | |
movq %rax, %rdi | |
call _ZNSolsEPFRSoS_E | |
movl $0, %edx | |
movl $1, %esi | |
movl $2, %edi | |
call socket | |
movl %eax, -120(%rbp) | |
cmpl $0, -120(%rbp) | |
jns .L2 | |
movl $.LC1, %esi | |
movl $_ZSt4cerr, %edi | |
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc | |
movl $_ZSt5flushIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi | |
movq %rax, %rdi | |
call _ZNSolsEPFRSoS_E | |
movl $1, %eax | |
movq -8(%rbp), %rdx | |
xorq %fs:40, %rdx | |
je .L5 | |
jmp .L6 | |
.L2: | |
movw $2, -48(%rbp) | |
movl $.LC2, %edi | |
call atoi | |
movzwl %ax, %eax | |
movl %eax, %edi | |
call htons | |
movw %ax, -46(%rbp) | |
movl $0, -44(%rbp) | |
leaq -48(%rbp), %rcx | |
movl -120(%rbp), %eax | |
movl $16, %edx | |
movq %rcx, %rsi | |
movl %eax, %edi | |
call bind | |
movl -120(%rbp), %eax | |
movl $1000, %esi | |
movl %eax, %edi | |
call listen | |
movl $.LC3, %esi | |
movl $_ZSt4cout, %edi | |
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc | |
movl $_ZSt5flushIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi | |
movq %rax, %rdi | |
call _ZNSolsEPFRSoS_E | |
movl $1347703880, -112(%rbp) | |
movl $825110831, -108(%rbp) | |
movl $808464928, -104(%rbp) | |
movl $1866664461, -100(%rbp) | |
movl $1852142702, -96(%rbp) | |
movl $2035559796, -92(%rbp) | |
movl $540697968, -88(%rbp) | |
movl $1954047348, -84(%rbp) | |
movl $1634496559, -80(%rbp) | |
movl $168652393, -76(%rbp) | |
movl $1699219981, -72(%rbp) | |
movl $544173164, -68(%rbp) | |
movl $1819438935, -64(%rbp) | |
movw $100, -60(%rbp) | |
.L4: | |
movl $16, -124(%rbp) | |
leaq -124(%rbp), %rdx | |
leaq -32(%rbp), %rcx | |
movl -120(%rbp), %eax | |
movq %rcx, %rsi | |
movl %eax, %edi | |
call accept | |
movl %eax, -116(%rbp) | |
movl $.LC4, %esi | |
movl $_ZSt4cout, %edi | |
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc | |
movl $_ZSt5flushIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi | |
movq %rax, %rdi | |
call _ZNSolsEPFRSoS_E | |
cmpl $0, -116(%rbp) | |
jns .L3 | |
movl $.LC5, %esi | |
movl $_ZSt4cerr, %edi | |
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc | |
movl $_ZSt5flushIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, %esi | |
movq %rax, %rdi | |
call _ZNSolsEPFRSoS_E | |
.L3: | |
leaq -112(%rbp), %rcx | |
movl -116(%rbp), %eax | |
movl $53, %edx | |
movq %rcx, %rsi | |
movl %eax, %edi | |
call write | |
movl -116(%rbp), %eax | |
movl %eax, %edi | |
call close | |
jmp .L4 | |
.L6: | |
call __stack_chk_fail | |
.L5: | |
leave | |
.cfi_def_cfa 7, 8 | |
ret | |
.cfi_endproc | |
.LFE970: | |
.size main, .-main | |
.type _Z41__static_initialization_and_destruction_0ii, @function | |
_Z41__static_initialization_and_destruction_0ii: | |
.LFB976: | |
.cfi_startproc | |
pushq %rbp | |
.cfi_def_cfa_offset 16 | |
.cfi_offset 6, -16 | |
movq %rsp, %rbp | |
.cfi_def_cfa_register 6 | |
subq $16, %rsp | |
movl %edi, -4(%rbp) | |
movl %esi, -8(%rbp) | |
cmpl $1, -4(%rbp) | |
jne .L7 | |
cmpl $65535, -8(%rbp) | |
jne .L7 | |
movl $_ZStL8__ioinit, %edi | |
call _ZNSt8ios_base4InitC1Ev | |
movl $_ZNSt8ios_base4InitD1Ev, %eax | |
movl $__dso_handle, %edx | |
movl $_ZStL8__ioinit, %esi | |
movq %rax, %rdi | |
call __cxa_atexit | |
.L7: | |
leave | |
.cfi_def_cfa 7, 8 | |
ret | |
.cfi_endproc | |
.LFE976: | |
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii | |
.type _GLOBAL__sub_I_main, @function | |
_GLOBAL__sub_I_main: | |
.LFB977: | |
.cfi_startproc | |
pushq %rbp | |
.cfi_def_cfa_offset 16 | |
.cfi_offset 6, -16 | |
movq %rsp, %rbp | |
.cfi_def_cfa_register 6 | |
movl $65535, %esi | |
movl $1, %edi | |
call _Z41__static_initialization_and_destruction_0ii | |
popq %rbp | |
.cfi_def_cfa 7, 8 | |
ret | |
.cfi_endproc | |
.LFE977: | |
.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main | |
.section .ctors,"aw",@progbits | |
.align 8 | |
.quad _GLOBAL__sub_I_main | |
.weakref _ZL20__gthrw_pthread_oncePiPFvvE,pthread_once | |
.weakref _ZL27__gthrw_pthread_getspecificj,pthread_getspecific | |
.weakref _ZL27__gthrw_pthread_setspecificjPKv,pthread_setspecific | |
.weakref _ZL22__gthrw_pthread_createPmPK14pthread_attr_tPFPvS3_ES3_,pthread_create | |
.weakref _ZL20__gthrw_pthread_joinmPPv,pthread_join | |
.weakref _ZL21__gthrw_pthread_equalmm,pthread_equal | |
.weakref _ZL20__gthrw_pthread_selfv,pthread_self | |
.weakref _ZL22__gthrw_pthread_detachm,pthread_detach | |
.weakref _ZL22__gthrw_pthread_cancelm,pthread_cancel | |
.weakref _ZL19__gthrw_sched_yieldv,sched_yield | |
.weakref _ZL26__gthrw_pthread_mutex_lockP15pthread_mutex_t,pthread_mutex_lock | |
.weakref _ZL29__gthrw_pthread_mutex_trylockP15pthread_mutex_t,pthread_mutex_trylock | |
.weakref _ZL31__gthrw_pthread_mutex_timedlockP15pthread_mutex_tPK8timespec,pthread_mutex_timedlock | |
.weakref _ZL28__gthrw_pthread_mutex_unlockP15pthread_mutex_t,pthread_mutex_unlock | |
.weakref _ZL26__gthrw_pthread_mutex_initP15pthread_mutex_tPK19pthread_mutexattr_t,pthread_mutex_init | |
.weakref _ZL29__gthrw_pthread_mutex_destroyP15pthread_mutex_t,pthread_mutex_destroy | |
.weakref _ZL30__gthrw_pthread_cond_broadcastP14pthread_cond_t,pthread_cond_broadcast | |
.weakref _ZL27__gthrw_pthread_cond_signalP14pthread_cond_t,pthread_cond_signal | |
.weakref _ZL25__gthrw_pthread_cond_waitP14pthread_cond_tP15pthread_mutex_t,pthread_cond_wait | |
.weakref _ZL30__gthrw_pthread_cond_timedwaitP14pthread_cond_tP15pthread_mutex_tPK8timespec,pthread_cond_timedwait | |
.weakref _ZL28__gthrw_pthread_cond_destroyP14pthread_cond_t,pthread_cond_destroy | |
.weakref _ZL26__gthrw_pthread_key_createPjPFvPvE,pthread_key_create | |
.weakref _ZL26__gthrw_pthread_key_deletej,pthread_key_delete | |
.weakref _ZL30__gthrw_pthread_mutexattr_initP19pthread_mutexattr_t,pthread_mutexattr_init | |
.weakref _ZL33__gthrw_pthread_mutexattr_settypeP19pthread_mutexattr_ti,pthread_mutexattr_settype | |
.weakref _ZL33__gthrw_pthread_mutexattr_destroyP19pthread_mutexattr_t,pthread_mutexattr_destroy | |
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3" | |
.section .note.GNU-stack,"",@progbits |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment