c# 윈폼에서 tcp/ip 통신 연결 확인 후 라즈베리파이의 db에 접속 할려고하니 소켓이 끊어져있다고 뜹니다. 통신 연결 확인하면 연결 성공이라고 계속뜨고 db접속말고 다른 이벤트를 실행할때는 서버에 정상적으로 데이터도 가집니다.
아래 코드 한번 확인 부탁드립니다.
추가로 계속 문제없이 통신하다가 갑자기 안되네요.
통신 연결 확인 이벤트
private void conbox_Click(object sender, EventArgs e)
{
if (Connected == true) return;
// 서버 ip 및 포트 번호
string SIP = Server_ip.Text;
int SPort = int.Parse(Server_port.Text);
// TCP통신, 서버 연결 성공하면 connected -> true로 변환
Client_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 서버 연결
try
{
Client_Socket.Connect(new IPEndPoint(IPAddress.Parse(SIP), SPort));
Connected = true;
}
catch
{
Connected = false;
Client_Socket.Close();
}
if (Connected == true) concheck_text.Text = "연결 성공";
else concheck_text.Text = "연결 실패";
}
db 접속 이벤트
private void DBconn_button_Click(object sender, EventArgs e)
{
// mariadb 정보
string dbip = DBserver_textBox.Text;
string dbpprt = DBport_textBox.Text;
string dbinfor = "Server=" + dbip + ";Port=" + dbpprt + ";Database=test;Uid=root;Pwd=1234";
DBconn = new MySqlConnection(dbinfor);
try
{
if (DBconn.Ping()==true) //기존 ping 체크 문구 없었을때 에러메시지 -> reading from the stream has failed.
{
ping_test.Text = "ping 됨";
DBconn.Open();
DBconn_label.Text = "DB 접속 성공";
}
else
{
ping_test.Text = "ping 안됨";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
[최초 등록일: ]
[최종 수정일: 12/22/2021]